安卓. Google API翻译 [英] Android. Google API Translate

查看:155
本文介绍了安卓. Google API翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在集成Google API Translate时遇到一些问题. 将依赖关系添加到gradle配置中:

I have some problems to integrate Google API Translate. Add to gradle configuration this dependecies:

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'
}

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

And use this sample to translate some text:

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:

But I always receive 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密钥?有什么主意吗?

But, if I use Browser key, that work success. Why I can't use my android API key in this feature? Any idea?

推荐答案

设置API密钥的过程现在更加清晰.默认情况下,您会创建一个通用API密钥,该密钥可在任何地方使用,但是您可以为密钥添加限制,例如Android软件包名称,HTTP引荐来源网址或IP地址.

The process for setting up an API Key is now more clear. By default, you create a generic API key that works everywhere, but you can add restrictions to the key such as Android package names, HTTP referrers or IP addresses.

有关具体信息,请参见适用于Android的OAuth 2.0说明,以及

For specific please see the client library documentation for OAuth 2.0 instructions for Android, and the Cloud Console support documentation for setting up API keys.

这篇关于安卓. Google API翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆