后端Django请求接口代码
# 接口调用凭证
token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + app_secret
token_request = requests.get(token_url)
token = json.loads(token_request.text)
print('access_token:', token['access_token'])
# 获取 Short Link
short_url = 'https://api.weixin.qq.com/wxa/genwxashortlink?access_token=' + token['access_token']
short_data = {
'page_url': '/pages/index/index',
'page_title': 'index',
}
print('请求url:', short_url)
print('参数:', short_data)
short_request = requests.post(short_url, short_data)
short_json = short_request.text
print('结果:', short_json)
输出结果
access_token: 52_j97dX8U0RLrHCk9i......
请求url: https://api.weixin.qq.com/wxa/genwxashortlink?access_token=52_j97dX8U0RLrHCk9i......
参数: {'page_url': '/pages/index/index', 'page_title': 'index'}
结果: {"errcode":47001,"errmsg":"data format error rid: 61cabef5-23807713-1c4a1ebd"}
试了好多次都是这个错
access_token 写在 short_data 里面也是同样的错误
文档也没找到说明的地方 是我请求哪里出错了吗?求大佬们指点
{"errcode":40066,"errmsg":"invalid url rid: 61cac3ac-552bb2b8-4b65150b"}
改了,换成了这个错
指定为json格式试试
headers = {'Content-Type': 'application/json'}
short_request = requests.post(url=short_url, data=short_data, headers=headers)