首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓。Google API Translate

安卓。Google API Translate
EN

Stack Overflow用户
提问于 2015-09-12 01:27:52
回答 1查看 1.2K关注 0票数 4

我有一些问题,以整合谷歌API翻译。添加到gradle配置此依赖项:

代码语言:javascript
复制
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'
}

并使用此示例翻译一些文本:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{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密钥?有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-01-28 01:56:33

现在,设置API密钥的过程更加清晰。默认情况下,您会创建一个通用API密钥,该密钥适用于任何地方,但您可以向密钥添加限制,如Android包名、HTTP引用或IP地址。

具体请参考OAuth 2.0 instructions for Android客户端库文档和setting up API keys云控制台支持文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32529017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档