test / apidemo /TranslateDemo.py
zwgao's picture
add app
5a79bd6
raw
history blame
1.03 kB
import requests
import os
from .utils.AuthV3Util import addAuthParams
# 您的应用ID
APP_KEY = os.environ.get("APP_KEY",None)
# 您的应用密钥
APP_SECRET = os.environ.get("APP_SECRET",None)
def createRequest(msq):
'''
note: 将下列变量替换为需要请求的参数
'''
q = msq
lang_from = 'auto'
lang_to = 'en'
# vocab_id = '您的用户词表ID'
data = {'q': q, 'from': lang_from, 'to': lang_to}
addAuthParams(APP_KEY, APP_SECRET, data)
header = {'Content-Type': 'application/x-www-form-urlencoded'}
res = doCall('https://openapi.youdao.com/api', header, data, 'post')
print(str(res.content, 'utf-8'))
return str(res.content, 'utf-8')
def doCall(url, header, params, method):
if 'get' == method:
return requests.get(url, params)
elif 'post' == method:
return requests.post(url, params, header)
# 网易有道智云翻译服务api调用demo
# api接口: https://openapi.youdao.com/api
if __name__ == '__main__':
createRequest()