按照接口文档的参数结构来简单写一个demo:
import json
import requests
def postRequest(url, data):
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.post(url, headers=headers, data=json.dumps(data))
response.encoding = "utf-8"
print(response.text)
def getRequest(url):
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.get(url, headers=headers)
response.encoding = "utf-8"
return response.json()
def get_token():
res = getRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxx&secret=xxxxxx")
print(res)
return res['access_token']
if __name__ == '__main__':
token = get_token()
url = "https://api.weixin.qq.com/wxa/book/createbook?access_token=" + token
data = {
'first_category_id': 10099,
'second_category_id': 10109,
'third_category_id': 10110,
'complete_status': 2,
'cover_media_id': 'xxxxxxxxxxxxxxxxxxxx',
'author': 'SALIERI',
'intro': '这是小说简介',
'title': "小说名字",
}
postRequest(url, data)
报错:
{'access_token': 'xxxxx', 'expires_in': 7200}
{"errcode":47001,"errmsg":"data format error rid: 68b5466f-6bc353ed-3e704d66"}
不知道哪里有问题,请赐教

后续有API的问题开发者可前往「API诊断工具」自助查询和定位哈,通过rid就可以自助定位了,很方便哒~https://developers.weixin.qq.com/console/api-center/index
你好,68b5466f-6bc353ed-3e704d66日志查看解决下中文乱码
{"first_category_id": 10099, "third_category_id": 10110, "cover_media_id": "BbusHSArkpXvmBwBIPYpIqVKSJGUm8dDh1Wd6OSpVlDeZJuozt6S2loNexe3guLv", "author": "\u5e0c\u6daf", "intro": "\u6bb5\u5982\u6b4c\u56e0\u4e3a\u8eab\u67d3\u91cd\u75be\uff0c\u5fcd\u75db\u548c\u7a77\u4e66\u751f\u7537\u53cb\u5206\u624b\uff0c\u8c01\u77e5\u9053\u5f53\u5979\u5ac1\u5165\u5143\u738b\u5e9c\u4e4b\u540e\u3002\u5374\u53d1\u73b0\u539f\u6765\u5143\u738b\u5c31\u662f\u81ea\u5df1\u7684\u524d\u7537\u53cb\u2026\u2026", "second_category_id": 10109, "complete_status": 2, "title": "\u5f03\u59be\u5982\u6b4c"}
你好,建议对照文档,重新检查一下请求参数
https://developers.weixin.qq.com/console/api-center/index?csrfRand=1933373635
是 json.dumps 的编码问题 改为 data=json.dumps(data, ensure_ascii=False).encode('utf-8') 解决(py3)在postman里面试试,怀疑是乱码导致的json格式有问题了