我正在努力学习Google如何与Java一起工作,但我很难找到如何输入API的授权密钥。API键指令要求使用key=API_KEY参数传递它,但我不确定输入的位置。这是添加到主类中还是在另一个子类中输入的?
我使用运行在Windows 10上的Eclipse作为我的平台。我的基本配置来自https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-java。按照这些步骤,我已经在Google注册并创建了API-Key,将.JSON文件下载到我的笔记本上,并安装了Google,并遵循了在这个campaign=ToolsforEclipse上配置的说明。
我用来测试的代码是在https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-java的Quickstart中找到的示例Java代码。
// Imports the Google Cloud client library
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
Translate translate = TranslateOptions.getDefaultInstance().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());
}
}我已经尝试修改我的变量来指向路径变量GOOGLE_APPLICATION_CREDENTIALS=c:_project\translation.json,但这似乎没有什么区别。
这是Eclipse在尝试运行程序时返回的错误消息,
Aug. 25, 2019 10:08:29 P.M.
com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.
Exception in thread "main" com.google.cloud.translate.TranslateException: The request is missing a valid API key.
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:62)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:156)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:124)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:121)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:120)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:138)
at com.example.translate.QuickstartSample.main(QuickstartSample.java:35)
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" } 发布于 2020-11-30 23:45:23
阅读文档--这是为谷歌云翻译添加api_key的方法。我对它进行了测试,它在两种情况下都有效。
“注意,这段代码也可以与API键一起使用。默认情况下,API键在GOOGLE_API_KEY环境变量中查找。一旦设置API密钥,就可以通过调用通过创建的翻译服务上的方法来调用API。
您还可以显式地设置API密钥,如下所示:
Translate translate = TranslateOptions.newBuilder().setApiKey("myKey").build().getService();
https://stackoverflow.com/questions/57735671
复制相似问题