问题描述
我正在尝试在我的应用程序中使用Google Cloud Translation API,但是每当我尝试翻译某些内容时,都会出现此缺失的有效API错误.
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.
我已经完成了快速入门步骤,但没有成功. https://cloud.google.com/translate/docs/quickstart
I've done the quickstart steps and that didn't work.https://cloud.google.com/translate/docs/quickstart
我已经尝试了客户端库身份验证中的步骤,但没有成功. https://cloud.google.com/translate/docs/reference/libraries
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
推荐答案
如果您正在使用客户端库,并且已经下载了服务帐户json文件,请尝试以下
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
}
);
代替
// Instantiates a client
const translate = new Translate({projectId});
这样,您只需要服务帐户json文件和启用的特定API
this way you need only your the service acount json file and the specific API enabled
这篇关于Google Cloud Translation API请求缺少有效的API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!