错题集(已解决):微信公众平台数据统计接口报错'errcode': 61500, 'errmsg': 'date format error hint: []'}

请求微信公众平台数据统计接口时,如下调用方式报错

from requests import post
from json import loads

header = {"Content-Type":"application/x-www-form-urlencoded"}  # 此句无用
period = {"begin_date": "2018-12-01", "end_date": "2018-12-03"}
url = 'https://api.weixin.qq.com/datacube/getusercumulate?access_token='+myAccessToken
mdl_rqt = post(url, data=period, headers=header, timeout=10)
mdl_rqt.encoding = "utf-8"
dct_rqt = loads(mdl_rqt.text)
print(dct_rqt)

得到:
{‘errcode’: 61500, ‘errmsg’: ‘date format error hint: […]’}
提示日期格式错误,但是日期格式是完全照抄手册的不会出错,于是考虑是编码解码的问题,乱搞一通。


解决:
结果是由于对方要求的data需要json格式或者说纯字符串,于是修改为如下:

from json import dumps  # 增加

header = {"Content-Type":"application/x-www-form-urlencoded"}  # 此句无用
# 有人说这里改成'Content-Type': 'application/json'也可以,实测并没有效果,这里"Content-Type"对后面没有影响
period = dumps({"begin_date": "2018-12-01", "end_date": "2018-12-03"})  # 增加dumps
# 也可以直接写period = '{"begin_date": "2018-12-01", "end_date": "2018-12-03"}'
url = 'https://api.weixin.qq.com/datacube/getusercumulate?access_token='+myAccessToken
mdl_rqt = post(url, data=period, headers=header, timeout=10)
mdl_rqt.encoding = "utf-8"
dct_rqt = loads(mdl_rqt.text)
print(dct_rqt)

参考: http://www.cnblogs.com/android-it/p/9558751.html

展开阅读全文

没有更多推荐了,返回首页