【问题标题】:bad request error 400 google api for translation python错误请求错误 400 用于翻译 python 的谷歌 api
【发布时间】:2017-02-21 06:50:33
【问题描述】:

调用 API 并得到错误需要从英语翻译成法语但得到这个错误所以我使用了这个代码但它不能正常工作

代码:

from __future__ import print_function

__author__ = 'jcgregorio@google.com (Joe Gregorio)'

from googleapiclient.discovery import build


def main():

  service = build('translate', 'v2',
            developerKey='AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0')
  print(service.translations().list(
      source='en',
      target='fr',
     q="flower"
    ).execute())

if __name__ == '__main__':
  main()

错误

Traceback (most recent call last):
  File "trail.py", line 19, in <module>
    main()
  File "trail.py", line 15, in main
    q="flower"
  File "build/bdist.linux-i686/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
  File "build/bdist.linux-i686/egg/googleapiclient/http.py", line 840, in execute
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/language/translate/v2?q=flower&source=en&alt=json&target=fr&key=AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0 returned "Bad Request">

【问题讨论】:

    标签: python google-translate


    【解决方案1】:

    使用新的 Google Cloud 客户端库并使用服务帐号凭据 JSON 授权您的 API,并调用 export GOOGLE_APPLICATION_CREDENTIALS=your_service.json 而不是使用 API 密钥。您还可以通过调用gcloud auth application-default login 来使用默认应用程序凭据。

    Google Cloud Translate Python samples 对此进行了演示,这也应该可以帮助您更快地开始。

    请注意,翻译 API 需要计费,如 described in the translate quickstart instructions,因此请准备好为您的项目进行设置。

    【讨论】:

    • 它要求启用计费是否有必要
    • 它要求启用计费是否有必要@class
    • 是的。您必须为某些高级 API 功能启用计费。
    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多