使用 JSON 的谷歌翻译高级 API



我是谷歌翻译api高级版和json的新手。我已将服务帐户和密钥凭据保存在 json 文件中。我想使用"nmt"模型。以下是我的蟒蛇代码。我可以获取访问令牌,但仍然无法使其正常运行。请让我知道我做错了哪一部分。我感谢您的帮助。

from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build
base_url = ['https://www.googleapis.com/language/translate/v2']
# load json credential keys
my_credentials = GoogleCredentials.from_stream('./data/TranslateAPI-cbe083d405fe.json')
# get access token 
access_token = my_credentials.get_access_token(base_url)
# build service
service = build('translate', 'v2', credentials=access_token, model='nmt')
text = u'So let us begin anew--remembering on both sides that civility is not a sign of weakness, and sincerity is always subject to proof. Let us nevernegotiate out of fear. But let us never fear to negotiate.'
test = service.translations().list(q=text, target='es')
results = test.execute()

我收到以下错误:

Traceback (most recent call last):
File "C:UsersyingworkspaceGoogleTranslateAPI_v3test1.py", line 32, in <module>
test = service.translations().list(q=text, target='es')
File "C:Anacondalibsite-packagesgoogleapiclientdiscovery.py", line 778, in method
headers, params, query, body = model.request(headers,
AttributeError: 'str' object has no attribute 'request'

您应该使用Google Cloud Translate Client您使用的客户端不支持"NMT"。在这种情况下,您遇到的错误是关于您在"模型"参数中输入了错误的值类型。正确的值应该是googleapiclient。型

最新更新