Android Question Cloud Translation API v3

RichardN

Well-Known Member
Licensed User
Longtime User
Is anyone using the Google Cloud Translation API successfully at Ver 3? I have some questions..

- Which of the myriad of API keys keys is required?
- Is there a working example of how to post the request and parse the result with this version?
 
Last edited:

RichardN

Well-Known Member
Licensed User
Longtime User
I have located the correct API here. Be sure to enable the service before generating an API key as the workflow is not altogether intuitive. However.... It would appear the API ver3 does not accept a key generated from the console so I am reverting to v2 but with little success.

I am basing my code on this snippet by @gregorio_adrian_gimiez from 2020 (there is nothing newer) but it is continually coming back with the text below. There seems to be something wrong with authentication as the app is apparently blocked

}
]
}
}

ResponseError. Reason: , Response: {
"error": {
"code": 403,
"message": "Requests from this Android client application \u003cempty\u003e are blocked.",
"errors": [
{
"message": "Requests from this Android client application \u003cempty\u003e are blocked.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "API_KEY_ANDROID_APP_BLOCKED",
"domain": "googleapis.com",
"metadata": {
"consumer": "projects/1097153xxxxx",
"service": "translate.googleapis.com"
}
}
]
}
}
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**

B4X:
    Dim translate As Map
    translate.Initialize
    translate.Put("target", "fr") '-> language
    translate.Put("q", "Good Morning) '- text translate
 
    Dim json As JSONGenerator
    json.Initialize(translate)
 
    Dim job As HttpJob:job.Initialize("@request.json", Me)
 
    Dim API_KEY As String="AIzxxxxxxxxxxxxxxxxxxxx" '<--APIKEY '

    Dim poststr As String = "https://translation.googleapis.com/language/translate/v2?key=" & API_KEY      
    job.PostString(poststr, json.ToString)
    job.GetRequest.SetContentType("application/json; charset=utf-8")

    Wait For (job) JobDone(job As HttpJob)

        If job.Success Then
            Try

                Dim jsonParser As JSONParser
                jsonParser.Initialize(job.GetString)
                Log(jsonParser.NextObject.Get("data")
 
            Catch

                Log(LastException)

            End Try

        Else

            Log(job.ErrorMessage)

        End If

 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you should return to the documentation to see the correct way to make a request.
there is a lot you left out.
example from the documentation:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_NUMBER_OR_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID:translateText"


putting a key and the request in the url as you did strikes me as how things were done in a much earlier version.
the current cloud versions (basic and advanced) don't even seem to mention the key.

i think there are some simpler, more direct services out there. depending on your use case.
 
Upvote 0
Top