【发布时间】:2015-10-13 16:46:07
【问题描述】:
我一直在互联网上到处搜索谷歌翻译 API 的使用,但我找不到下降教程或解释。所以,这就是我所做的:
在我的 Google API 控制台中,我使用this 答案在我的 SHA1 指纹的公共 API 访问下生成了一个密钥。这是我的 API 控制台的样子:
在 Android 工作室中,我使用 OkHttp 库和以下代码构建并发送我的请求:
OkHttpClient client = new OkHttpClient();
String apiKey = "My API key";
String apiLangSource = "en";
String apiLangTarget = "de";
String apiWord = "Hello";
String googleApiUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&source=" + apiLangSource + "&target=" + apiLangTarget + "&q=" + apiWord;
Request request = new Request.Builder().url(googleApiUrl).build();
Log.d(TAG, "API STRING" + googleApiUrl);
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG , "HTTP CALL FAIL");
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG , response.body().string());
}
});
它运行良好,但响应我得到:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "ipRefererBlocked",
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
"extendedHelp": "https://console.developers.google.com"
}
],
"code": 403,
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
}
}
这里有什么问题?我的 API 设置是否正确?我是否正确拨打电话(我看过一些图书馆但有指南)?这是使用这个库的合理方式吗?这究竟是什么意思?
"There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
我认为有一些免费的演示电话可用,这不是问题所在。
【问题讨论】:
-
我猜你的 API 配置没有做好。你可以看到回复
restriction configured on your API key and the request does not match -
当我移除指纹并打包时,它可以工作。所以这确实是身份验证错误。谷歌如何验证我的指纹和包裹?
-
我遇到了同样的问题。 @DannyBabbev,你找到解决方案了吗?
-
我还没有找到解决办法。
标签: android google-api google-translate