微信公眾號中通過菜單事件獲得當前用戶信息


首先、配置好網頁授權:

自定義菜單的格式:

                {
                    "type": "view",
                    "name": "注冊賬號",
                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{register-url}&response_type=code&scope=snsapi_base#wechat_redirect"
                },

其中需要替換的2個參數:#{appid}和#{register-url},除去這兩個參數外其它的部分為微信要求的格式!

當用戶點擊“注冊賬戶”菜單后,系統會調用上邊替換的register—url地址,並且會在這個地址添加一個code參數,通過這個code可以從微信平台拿到用戶的openid,拿到openid后通過我們系統記錄的綁定關系,就可以拿到userid了。

代碼如下:

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Const.appid + "&secret=" + Const.secret + "&code=" + code
                + "&grant_type=authorization_code";
        String content = NetworkUtils.doGetByHttps(url);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM