微信获取用户数据后台写法,author2.0认证

 1 /* 微信授权接口 */
 2 //1、设置路由
 3 router.get(‘/wechat/userinfo‘, function(req, res) {
 4     var cb = req.query.cb;
 5     //设置cookie
 6     res.cookie(wechat_userinfo_callback_url, cb);
 7     var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://m.moretao.com/wechat/userinfo/callback&response_type=code&scope=snsapi_userinfo#wechat_redirect";
 8     //2、重定向到拼接url
 9     res.redirect(url);
10 });
11 //3、重定向的url经过微信服务器转到如下地址;
12 router.get(‘/wechat/userinfo/callback‘, function(req, res) {
13     //获得code
14     var code = req.query.code;
15     //用code拼接url
16     var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
17     //4、后台用request请求数据;目的是为了获取刷新token
18     request.get(url, function(error, response, body) {
19         //获得刷新用的token值
20         var json = JSON.parse(body);
21         //用得到的刷新token值拼接url
22         var refresh_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + json.refresh_token;
23         //5、后台用request请求数据;为了获取最终access_token
24         request.get(refresh_url, function(error, response, refresh) {
25             var json = JSON.parse(refresh);
26             //用access_token拼接url
27             var info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + json.access_token + "&openid=" + json.openid + "&lang=zh_CN";
28             //6、后台用request请求数据,获取用户数据;
29             request.get(info_url, function(error, response, info) {
30                 //通过cookie取出开始传入的url地址
31                 var callback = req.cookies[wechat_userinfo_callback_url];
32                 //将数据拼接到url中,并且将数据用encodeURLComponent转码;
33                 var str = "?data=";
34                 if(callback.indexOf(‘?‘) > -1) str = "&data=";
35                 var ret = callback + str + encodeURIComponent(info);
36                 //7、重定向到目标url并加上转码的数据,前端得到后需要转码处理
37                 res.redirect(ret);
38             });
39         });
40     });
41 });

 

微信获取用户数据后台写法,author2.0认证

上一篇:uni-app使用阿里矢量字体图标


下一篇:使用url的时候添加app_name,提示错误:Specifying a namespace in include() without providing an app_name ' django.core.exceptions.ImproperlyConfigured: Specifying a ...