• 百度聊天机器人UNIT http访问


    #-*- 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'])
    

      

  • 相关阅读:
    新博客
    【Gym-100712 #H】Bridges
    【CodeForces817F】MEX Queries
    【POJ1734】Sightseeing trip
    【Aizu2968】Non-trivial Common Divisor
    【Gym-101473 #I】Patches
    【POJ2228】Naptime
    【CodeForces219D】Choosing Capital for Treeland
    【URAL1018】Binary Apple Tree
    深入探索C++对象模型(五)
  • 原文地址:https://www.cnblogs.com/xuyong437/p/11365895.html
Copyright © 2020-2023  润新知