gpt4 book ai didi

google-translate - 安卓。谷歌 API 翻译

转载 作者:行者123 更新时间:2023-12-02 21:47:22 24 4
gpt4 key购买 nike

我在集成 Google API Translate 时遇到一些问题。添加到 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 key ?有什么想法吗?

最佳答案

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

具体请参见 OAuth 2.0 instructions for Android 的客户端库文档,以及 setting up API keys 的 Cloud Console 支持文档.

关于google-translate - 安卓。谷歌 API 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32529017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com