I'm trying to use the Google Cloud Translation API in my application but whenever I try to translate something it comes up with this missing valid API error.
I've done the quickstart steps and that didn't work. https://cloud.google.com/translate/docs/quickstart
I've tried the steps in the client library authentication and that hasn't worked. https://cloud.google.com/translate/docs/reference/libraries
E/AndroidRuntime: FATAL EXCEPTION: main
Process: herrsa1.bit.translator, PID: 16598
com.google.cloud.translate.TranslateException: The request is missing a valid API key.
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:144)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:113)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:110)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:110)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:124)
at herrsa1.bit.translator.MainActivity.translateText(MainActivity.java:61)
at herrsa1.bit.translator.MainActivity$1.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11180)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "The request is missing a valid API key.",
"reason" : "forbidden"
} ],
"message" : "The request is missing a valid API key.",
"status" : "PERMISSION_DENIED"
}
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:130)
... 19 more
The error on API key means you didn't create or use the key properly. You need to do the following for the key to work:
1) create a service account [1]
2) create a key for above service account [2]
3) download the key to a location, for example, a local path
4) set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the key, refer to samples in Quickstart tutorial
Doing 1) and 2) in GCP Console, 3) and 4) in Cloud Shell would be the easiest.
[1] https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating_a_service_account
[2] https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys
If you are using client library and you have already downloaded your service account json file try below
// Instantiates a client
const translate = new Translate(
{
projectId: 'your project id', //eg my-project-0o0o0o0o'
keyFilename: 'path of your service acount json file' //eg my-project-0fwewexyz.json
}
);
instead of
// Instantiates a client
const translate = new Translate({projectId});
this way you need only your the service acount json file and the specific API enabled
I also tried to execute this sample program. I followed the same instruction. But when I executing I got same error(The request is missing a valid API key).
I changed a line in the sample program.
Instead of
Translate translate = TranslateOptions.getDefaultInstance().getService();
I added
Translate translate = TranslateOptions
.newBuilder()
.setCredentials(
ServiceAccountCredentials
.fromStream(new FileInputStream(
"YourCredentialFilePath.json")))
.build().getService();
Now it is working.
Sample code after fix.
// Imports the Google Cloud client library
import java.io.FileInputStream;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class QuickstartSample {
public static void main(String... args) throws Exception {
//Instantiates a client
//Removed next line
//Translate translate = TranslateOptions.getDefaultInstance().getService();
//Added this line
Translate translate = TranslateOptions
.newBuilder()
.setCredentials(
ServiceAccountCredentials
.fromStream(new FileInputStream(
"YourCredentialFilePath.json")))
.build().getService();
//The text to translate
String text = "Hello, world!";
//Translates some text into Russian
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
}
来源:https://stackoverflow.com/questions/50567647/google-cloud-translation-api-the-request-is-missing-a-valid-api-key