谷歌翻译403错误

时间:2015-12-03 21:05:23

标签: android google-translate

我正在尝试使用Google Translate API 创建发布的Android应用版本,但不断收到以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
  {
    "code" : 403,
    "errors" : [ {
      "domain" : "usageLimits",
      "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.",
      "reason" : "ipRefererBlocked",
      "extendedHelp" : "https://console.developers.google.com"
    } ],
    "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."
}

使用服务器密钥在Debug中完美运行。我已经创建了一个API密钥和一个OAuth 2.0客户端ID。

我使用以下代码来获取请求(使用google's translate library):

    // init
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleClientRequestInitializer googleClientRequestInitializer = new TranslateRequestInitializer(key);

    Translate.Builder translateBuilder = new Translate.Builder(httpTransport, jsonFactory, null);
    translateBuilder.setApplicationName(applicationName);
    translateBuilder.setGoogleClientRequestInitializer(googleClientRequestInitializer);

    // Build the translator
    Translate translate = translateBuilder.build();

    // Create a list of the text to translate
    List<String> text = new ArrayList<String>();
    text.add("Hello");

    // Translate text
    Translate.Translations.List list = translate.translations().list(text, targetLanguage);
    list.setFormat("text");

    TranslationsListResponse translateResponse = list.execute();

非常感谢任何帮助。谢谢!

0 个答案:

没有答案