百度聊天机器人UNIT http访问

孤街浪徒 提交于 2019-11-27 13:34:47

 

 

#-*- version: Python3.0 -*
#-*- coding: UTF-8      -*
import urllib
import urllib.request
import sys
import ssl
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=eiBsX7AmncXIANL7yCGojkTA&client_secret=OnqEu2UEfSd6aPA7evwHYGg5EFRE0Tia'
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
resp_taken = response.read()

#if (resp_taken):
#    print(resp_taken)

text = json.loads(resp_taken)
#print(text["access_token"])

headers = {'Content-Type':'application/json'}
access_token = text["access_token"]
url = 'https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=' + access_token
post_data = "{\"log_id\":\"UNITTEST_10000\",\"version\":\"2.0\",\"service_id\":\"S20837\",\"session_id\":\"\",\"request\":{\"query\":\"你几岁了\",\"user_id\":\"88888\"},\"dialog_state\":{\"contexts\":{\"SYS_REMEMBERED_SKILLS\":[\"1057\"]}}}"
request = urllib.request.Request(url,data=post_data.encode('utf-8'),headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode("utf-8")

#if content:
#    print(content)

text1 = json.loads(content)
print(text1['result']['response_list'][0]['action_list'][0]['say'])

  

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!