import requests # client_id 为官网获取的AK, client_secret 为官网获取的SK host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=hIkC0fSxfQX17dGsbVcUGYzx&client_secret=wluN6dVLGjUlnyvxtiSzQzchrHnv4O2K' response = requests.get(host) if response: text_dict = response.json() access_token = text_dict['access_token'] print(access_token) 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":1,"client_session":"{\"client_results\":\"\", \"candidate_options\":[]}","query":"格力空调价格怎么样?","query_info":{"asr_candidates":[],"source":"KEYBOARD","type":"TEXT"},"updates":"","user_id":"88888"},"bot_id":"1042747","version":"2.0"}".encode("utf-8") headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(url, data=post_data, headers=headers) if response: print (response.json())