简体   繁体   English

通过json使用Google翻译高级API

[英]use google translate premium API by json

I am new to google translate api premium edition and json. 我是Google翻译api高级版和json的新手。 I have the service account and the key credentials save in a json file. 我有服务帐户和密钥凭证保存在json文件中。 I want to use the 'nmt' model. 我想使用“ nmt”模型。 The following are my python code. 以下是我的python代码。 I can get the access token but still cannot make it run correctly. 我可以获得访问令牌,但仍然无法使其正确运行。 Please let me know which part I did wrong. 请让我知道我做错了哪一部分。 I appreciate your help. 我感谢您的帮助。

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

I got the following errors: 我收到以下错误:

Traceback (most recent call last):
File "C:\Users\ying\workspace\GoogleTranslateAPI_v3\test1.py", line 32, in <module>
test = service.translations().list(q=text, target='es')
File "C:\Anaconda\lib\site-packages\googleapiclient\discovery.py", line 778, in method
headers, params, query, body = model.request(headers,
AttributeError: 'str' object has no attribute 'request'

You should use Google Cloud Translate Client The client you are using doesn't support "NMT". 您应该使用Google Cloud Translate客户端您使用的客户端不支持“ NMT”。 The error you got in this case is about you entered wrong type of value to "model" parameter. 在这种情况下,您得到的错误是关于您向“模型”参数输入了错误的值类型。 The correct value should be a googleapiclient.Model 正确的值应该是googleapiclient.Model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM