路飞项目-微信推送

流程步骤

        1  关注路飞学城微信服务号
        
        2  生成一个微信认证二维码路径(注意参数)
        
              access_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={appid}&redirect_uri={redirect_uri}&response_type=code&scope=snsapi_userinfo&state={state}#wechat_redirect"
              
                access_url = access_url.format(
                    appid=settings.WECHAT_CONFIG["app_id"],
                    redirect_uri=settings.WECHAT_CONFIG["redirect_uri"],
                    state=123
                )
                ret['data'] = access_url
                
                
        3 在回调函数callback中:   获取token,再认证
        
            def callback():
            
                code = request.GET.get("code")
                # 用户UID
                state = request.GET.get("state") # 123

                # 获取该用户openId(用户唯一,用于给用户发送消息)
                res = requests.get(
                    url="https://api.weixin.qq.com/sns/oauth2/access_token",
                    params={
                        "appid": 'wx89085e915d351cae',
                        "secret": '64f87abfc664f1d4f11d0ac98b24c42d',
                        "code": code,
                        "grant_type": 'authorization_code',
                    }
                ).json()
                # 获取的到openid表示用户授权成功
                
            openid = res.get("openid")
            if openid:
                models.UserInfo.objects.filter(uid=request.session.get("uid")).update(wx_id=openid)
                response = "<h1>授权成功 %s </h1>" % openid

        4 推送消息
        
            1 获取token
            
            2 设置请求数据
            
            3 按格式发

文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

原文地址:https://www.cnblogs.com/benson321/p/9754976.html