微信小程序接口報錯47001的解決方法

使用微信小程序碼的生成接口,發現死活都報47001錯誤。
有人提到是urlencode的問題,不能使用urlencode。
修改了代碼,能夠了。代碼貼出來:json

import urllib.request
import urllib.parse
import json
#這個是微信獲取小程序碼的接口
url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=xxxxx'
#準備一下頭
headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
#用Post傳值,這裏值用JSON的形式
values = {"path": "?from=1"}
#將字典格式化成能用的形式,urlencode不能用
#data = urllib.parse.urlencode(values).encode('utf-8')
#使用json.dumps的方式序列化爲字符串,而後bytes進行編碼
data = json.dumps(values)
data=bytes(data,'utf8')
#建立一個request,放入咱們的地址、數據、頭
request = urllib.request.Request(url, data, headers)
#將獲取的數據存在本地文件
readData = urllib.request.urlopen(request).read()
f=open("wx.jpg","wb")
f.write(readData)
f.close()

參考:
https://blog.csdn.net/kingov/article/details/78742183小程序

相關文章
相關標籤/搜索