Wechat Official Document Repository Address
npm install node-wechat-oauth --save
const oauth = require('node-wechat-oauth');
const client = oauth({
openid:'',
secret:''
});
client.getAuthUrl(redirect, 'snsapi_base');
client.getAuthUrl(redirect, 'snsapi_userinfo');
client.getQRCodeAuthUrl(redirect);
getAccessToken(code, function(error, result){
console.log(error || result);
});
result:
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
client.getUserInfo({
access_token: 'ACCESS_TOKEN',
openid: 'OPENID'
},function(error, result){
console.log(error || result);
});
{
"openid":" OPENID",
"nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
client.refreshToken(access_token, function(error, result){
console.log(error, result);
});
client.verifyToken(access_token,openid,function(error, result){
console.log(error || result);
});
valid
{
"errcode":0,
"errmsg":"ok"
}
invalid
{
"errcode":40003,
"errmsg":"invalid openid"
}