python后台获取微信openid

1、小程序前端:

  get_openid: function(){
    var that = this
        
    wx.login({  
        success(res) {
        if (res.errMsg == 'login:ok') {
    
        wx.request({   
            url: 'https://www.********.top/openid',
            data: {
                tar_table: 'get_openid',
                appid: app.globalData.appid,
                app_secret: app.globalData.AppSecret,
                code: res.code,
            },  
            header: {
                'content-type': 'application/json',
            }, 
                
            success: function (res) {  
                app.globalData.openid = res.data.openid
                console.log("在home.js中获取openid:",app.globalData.openid)
                that.getShopInfo()

            },
            fail: console.error
        })

        } 
    }
    })
  }, 

其中,AppSecret去小程序管理控制台通过管理员授权生成。

二、python后台

def get_openid():
    v_param = request.args
    appid = v_param.get("appid")
    app_secret = v_param.get("app_secret")
    code = v_param.get("code")

    code2Session="https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code"

    code_url = code2Session.format(appid,app_secret,code)
    response = requests.get(code_url)  # 返回的是json数据
    json_response = response.json()    # 把json数据转换为字典
    print("返回结果:",json_response)
    if json_response.get('session_key'):
        return json_response
    else:
        return False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangan094

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值