【发布时间】:2015-12-08 08:13:15
【问题描述】:
我在集成 Google API 翻译时遇到了一些问题。 将此依赖项添加到 gradle 配置中:
compile 'com.google.apis:google-api-services-translate:v2-rev41-1.20.0'
compile ('com.google.api-client:google-api-client-android:1.20.0') {
exclude module: 'httpclient'
}
compile ('com.google.http-client:google-http-client-gson:1.20.0') {
exclude module: 'httpclient'
}
并使用此示例翻译一些文本:
final Translate translate = new Translate.Builder(
AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(),
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
Log.d(TAG, "Http requst: " + httpRequest);
}
})
.setTranslateRequestInitializer(new TranslateRequestInitializer("ANDROID_API_KEY"))
.build();
try {
String shot = PreferenceManager.getLanguage(App.getInst()).getLanguageShot();
Translate.Translations.List request = translate.translations().list(Arrays.asList(text), shot);
TranslationsListResponse tlr = request.execute();
List<TranslationsResource> list = tlr.getTranslations();
result = list.get(0).getTranslatedText();
} catch (IOException e) {
e.printStackTrace();
}
但我总是收到 IOException:
{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."}}
但是,如果我使用浏览器键,那工作就成功了。为什么我不能在此功能中使用我的 android API 密钥?有什么想法吗?
【问题讨论】:
-
发布您的 IOException 的详细信息
-
我已发布结果 JSON 消息。
-
@Nick 您能否提供一个链接,指向与 Android API 密钥一起使用的 Google Translate API 示例?
-
Translate API 客户端库的 documentation 具有 Android 特性,您还可以在 Stack Overflow 上找到有关该主题的 Q&A's。
标签: google-translate translate