百度翻译错误提示 {‘error_code‘: ‘52003‘, ‘error_msg‘: ‘UNAUTHORIZED USER‘}

这是我正确的代码:

import hashlib
import requests

appid = 'xxx'
salt = '1435660281'
secretKey = 'xxx'
queryWord = input('Please enter the English word that you want to translate into Chinese: ')

signNum = appid + queryWord + salt + secretKey
md = hashlib.md5()
md.update(signNum.encode('utf8'))
sign=md.hexdigest()

url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q=%s&from=en&to=zh&appid=%s&salt=%s&sign=%s'%(queryWord,appid,salt,sign)
cookie_str = 'xxxx'
cookie = {i.split('=')[0]:i.split('=')[1]  for i in cookie_str.split('; ')}
response = requests.post(url=url,headers={'Content-Type':'application/x-www-form-urlencoded','User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'},cookies=cookie)
print(response.json())

结果:

PS C:\Users\hp\Desktop\临时> python .\sign.py
Please enter the English word that you want to translate into Chinese: ball
{'from': 'en', 'to': 'zh', 'trans_result': [{'src': 'ball', 'dst': '球'}]}


当初返回 {‘error_code’: ‘52003’, ‘error_msg’: ‘UNAUTHORIZED USER’} 原因是url 拼接错误:

url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q=%s&from=en&to=zh&appid=%s&salt=%s&sign=%s'.format(queryWord,appid,salt,sign)

网上这方面资料少,我补充一下,要优先检查自己的代码是否能完全正确运行而不是先想是否平台出问题。

openRefine调用百度翻译api 模板

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页