2.0 python demo
zz_dev 发布于2018-05 浏览:1479 回复:1
0
收藏
快速回复
python方便点用来测试不错
token有效期是30天,可以自己加一个时间戳判断要不要重新获取.
 
#!/ Baidu Unit demo
# -*- coding: utf-8 -*-

import urllib, urllib2, sys
import ssl

# get access_token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API_KEY】&client_secret=【SECRET_KEY】'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
    print(content)

#eval to dictionary
content_dir = eval(content)
#token
access_token = content_dir['access_token']
print(access_token)

#post query
url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=' + access_token
post_data = "{\"bot_session\":\"\",\"log_id\":\"7758521\",\"request\":{\"bernard_level\":0,\"client_session\":\"{\\\"client_results\\\":\\\"\\\", \\\"candidate_options\\\":[]}\",\"query\":\"【query string】\",\"query_info\":{\"asr_candidates\":[],\"source\":\"KEYBOARD\",\"type\":\"TEXT\"},\"updates\":\"\",\"user_id\":\"88888\"},\"bot_id\":【bot id】,\"version\":\"2.0\"}"
request = urllib2.Request(url, post_data)
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request)
content = response.read()

if (content):
    print content.decode('utf-8').encode('gb18030')
收藏
点赞
0
个赞
共1条回复 最后由用户已被禁言回复于2022-04
#2伊茨米可回复于2018-05

干货~

0
TOP
切换版块