使用json的google translate premium API

时间:2021-06-01 14:50:48

I am new to google translate api premium edition and json. I have the service account and the key credentials save in a json file. I want to use the 'nmt' model. The following are my python code. 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.

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

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'

1 个解决方案

#1


0  

You should use Google Cloud Translate Client The client you are using doesn't support "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

您应该使用Google Cloud Translate客户端您使用的客户端不支持“NMT”。在这种情况下你得到的错误是关于你输入错误类型的值“模型”参数。正确的值应该是googleapiclient.Model

#1


0  

You should use Google Cloud Translate Client The client you are using doesn't support "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

您应该使用Google Cloud Translate客户端您使用的客户端不支持“NMT”。在这种情况下你得到的错误是关于你输入错误类型的值“模型”参数。正确的值应该是googleapiclient.Model