微信授权获取用户基本信息,支持express和koa,采用co函数实现代码同步执行
var oauth = require("wxoauth");
router.get('/', function(req, res, next) {
var redirect_url = 'http://' + req.headers.host + '/oauth/callback';
redirect_url += '?callback_url=' + req.originalUrl;
var callback_url = oauth.getAuthorizeURL(redirect_url, appid, 'snsapi_userinfo');
res.redirect(callback_url);
return;
});
var oauth = require("wxoauth");
var code = req.query.code;
var callback_url = req.query.callback_url;
oauth.getUserInfo(code, appid, secret).then(function(userinfo){
//保存用户授权后的数据
req.session.openid = userinfo.openid;
req.session.save();
res.redirect(callback_url);
});
========generator(koa2调用说明)======
var oauth = require("wxoauth");
router.get('/', async function(ctx, next) {
var redirect_url = 'http://' + ctx.host + '/oauth/callback'; //跳转路由
redirect_url += '?callback_url=' + urlencode(ctx.originalUrl);
var callback_url = oauth.getAuthorizeURL(redirect_url, appid, 'snsapi_userinfo');
ctx.redirect(callback_url);
});
var oauth = require("wxoauth");
router.get('/callback', async function(ctx, next){
var code = ctx.query.code;
var callback_url = ctx.query.callback_url;
var userinfo = await oauth.getUserInfo(code, appid, secret);
})