四时宝库

程序员的知识宝库

python获取微信小程序参数二维码(小程序调用python)

微信小程序分享时,有个需求就是分享获取积分,这个就需要分享小程序时携带特定用户参数,微信提供了参数二维码。

获取参数二维码首先需要获取token,具体接口是:

GET https://api.weixin.qq.com/cgi-bin/token


请求参数

属性

类型

必填

grant_type

string

appid

string

secret

string

返回参数

属性

类型

access_token

string

expires_in

number


然后才能通过token去获取二维码

POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN 


请求参数

属性

类型

必填

access_token

string

scene

string

返回参数

属性

类型

buffer

buffer

errcode

number

errmsg

string

获取二维码代码如下:

import Config
import requests
import json

class WechatMini:
    def __init__(self):
        self.config = Config.appConfig.GetWechatMiniConfig()
        

    def GetWechatMiniInfo(self, strCode:str) ->dict:
        '''获取openid'''
        strUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code".format(self.config['APPID'], self.config['APPSECRET'], strCode)
        resp = requests.get(strUrl)
        if resp.status_code != 200:
            return {"errorMsg": '请求错误', "errorNum": resp.status_code}
        resJson = resp.json()
        #print(strCode)
        #print(resJson)
        if 'errcode' in resJson:
            return {"errorMsg": resJson['errmsg'], "errorNum": resJson['errcode']}
        
        return {"session_key": resJson['session_key'], "openid": resJson['openid'], 'unionid': resJson['unionid']}
    

      

    def GetAccessToken(self) -> dict:
        '''获取token'''
        strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}".format(self.config['APPID'], self.config['APPSECRET'])
        resp = requests.get(strUrl)
       
        if resp.status_code != 200:
            return {"errorMsg": '请求错误', "errorNum": resp.status_code}
        resJson = resp.json()
        #print(strCode)
        print(resJson)
        if 'errcode' in resJson:
            return {"errorMsg": resJson['errmsg'], "errorNum": resJson['errcode']}
        
        return {"access_token": resJson['access_token'], "expires_in": resJson['expires_in']}
    
    def GetQrcode(self, unionid:str) ->bytes:
        '''获取二维码'''
        tokenInfo = self.GetAccessToken()
        if 'access_token' not in tokenInfo.keys():
            return {"errorMsg": tokenInfo['errorMsg'], "errorNum": tokenInfo['errorNum']}
        strUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}".format(tokenInfo['access_token'])
        postData = {
          "scene": 's=' + unionid, # 限定32个字符
          "page": 'pages/index/index'
        }
        resp = requests.post(strUrl, data=json.dumps(postData))

        return resp.content
        
  
  

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接