Google Translate API:来自此客户端应用程序的请求< empty>被封锁 [英] Google Translate API: Requests from this client application <empty> are blocked
问题描述
我一直收到错误403-使用付费的Google Cloud Platform Translation API阻止了来自此Android客户端应用程序的请求.将限制设置为无时,效果很好.报告类似问题有一些线索,但没有一个答案.
I've been getting error 403 - Requests from this Android client application are blocked using paid Google Cloud Platform Translation API. Works great when restrictions are set to none. There are a few threads around reporting similar issue, but none answered.
我在某些示例中看到过,有一个具有.setApplicationName()的版本,我认为这可能会有所帮助,但是我找不到那个版本.
I've seen in some examples, there's a version which had .setApplicationName(), I think that might help, but I can't find which version would that be.
使用的代码是:`
private void translate(String textToTranslate, String targetLanguage, TranslateCallback callback) {
try {
TranslateOptions options = TranslateOptions.newBuilder()
.setApiKey( < api_key >)
.build();
Translate trService = options.getService();
Translation translation = trService.translate(textToTranslate,TranslateOption.targetLanguage(targetLanguage));
callback.onSuccess(translation.getTranslatedText());
}
catch(Exception e) {
callback.onFailure();
}
}`
来自: https://medium.com/@ amsanjeev/adding-translate-api-to-android-apps-788c5bca5521
推荐答案
最后我找到了一种方法.
Finally I found a way.
在为Android应用设置API密钥限制时,您指定了程序包名称和SHA-1证书指纹.因此,当您向Google发送请求时,必须在每个请求的标题中添加这些信息.
When setting up your API key restriction for android app, you specified the package name and SHA-1 certificate fingerprint. So when you send an request to Google, you must add these information in the header of each request.
connection.setRequestProperty("X-Android-Package", "com.example.awesomeapp");
String sign = "5D:5A:12:D3:......".toLowerCase(); //Your SHA1 key in lower cased.
connection.setRequestProperty("X-Android-Cert", sig);
可以找到详细的答案这里.
这篇关于Google Translate API:来自此客户端应用程序的请求< empty>被封锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!