Google Translate API: Requests from this client application <empty> are blocked

有些话、适合烂在心里 提交于 2021-02-19 08:27:24

问题


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.

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.

Code used is:`

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();
    }
}`

from: https://medium.com/@amsanjeev/adding-translate-api-to-android-apps-788c5bca5521


回答1:


Finally I found a way.

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);

A detailed answer can be found here.



来源:https://stackoverflow.com/questions/49958986/google-translate-api-requests-from-this-client-application-empty-are-blocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!