Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信授权OAuth #20

Open
zp1112 opened this issue Jun 25, 2018 · 0 comments
Open

微信授权OAuth #20

zp1112 opened this issue Jun 25, 2018 · 0 comments
Labels

Comments

@zp1112
Copy link
Owner

zp1112 commented Jun 25, 2018

微信授权分为四大步骤

前端获取。用户进入A站,
一、redrect到授权页,带上回调页面A,用户点击允许授权
二、回到A页面,带上获取到的code
三、使用code请求获取accesstoken和openid
四、使用accesstoken和openid获取用户信息

后端获取。用户进入A站,请求login接口
一、login接口redrect到授权页,回调接口getAccesstoken,允许授权
二、回到getAccesstoken接口,带上获取到的code
三、使用code请求获取accesstoken和openid
四、使用accesstoken和openid获取用户信息
五、种下cookie,返回到页面A

// auth.js
var req = require('request');

/* 微信登陆 */
var AppID = 'xxx';
var AppSecret = 'xxxxxx';

module.exports = [{
  method: 'GET',
  path: '/wx_login',
  handler: async (request, h) => {
    // 第一步:用户同意授权,获取code
    var router = 'get_wx_access_token';
    // 这是编码后的地址
    var return_uri = 'http%3A%2F%2Fwww.test.com%2F'+router;  
    var scope = 'snsapi_userinfo';
    // 授权页,回调地址get_wx_access_token
    return h.redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid='+AppID+'&redirect_uri='+return_uri+'&response_type=code&scope='+scope+'&state=STATE#wechat_redirect');
  }
}, {
  method: 'GET',
  path: '/get_wx_access_token',
  handler: async (request, h) => {
    // 第二步:通过code换取网页授权access_token
    var code = request.query.code;
    req.get(
        {   
            url:'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+AppID+'&secret='+AppSecret+'&code='+code+'&grant_type=authorization_code',
        },
        function(error, response, body){
            if(response.statusCode == 200){
                // 第三步:拉取用户信息(需scope为 snsapi_userinfo)
                var data = JSON.parse(body);
                var access_token = data.access_token;
                var openid = data.openid;
                req.get(
                    {
                        url:'https://api.weixin.qq.com/sns/userinfo?access_token='+access_token+'&openid='+openid+'&lang=zh_CN',
                    },
                    function(error, response, body){
                        if(response.statusCode == 200){
                            // 第四步:根据获取的用户信息进行对应操作
                            var userinfo = JSON.parse(body);
                            // 可以由此创建一个帐户
                            h.response(userinfo.nickname)
                        }else{
                            console.log(response.statusCode);
                        }
                    }
                );
            }else{
                console.log(response.statusCode);
            }
        }
    );
  }
}];
@zp1112 zp1112 added the wechat label Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant