目录
1.在微信后台添加回调域名
_. 获取授权
_. 微信后台填写域名回调
- API 1: 设置分享最终连接
- API 2: 分享连接兼容回调
- API 3: 获取 access_token
- API 4: 刷新 access_token
- API 5: 拉取用户信息
授权信息
获取授权
var appid = “”;
var secret = “”;在微信后台添加回调域名
比如我们填写 game.baidu.com (当然, 你需要自己的域名)
API 1: 此链接作为最终分享用连接, state 参数为”msgid,userid”格式数据
var redirect_uri = "http://game.baidu.com/uri";
var scope = "snsapi_userinfo"; // snsapi_base 获取openid, snsapi_userinfo 详细用户信息
var state = ""; // 重定向会需要该参数
var authorizeURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect"
var url = util.format(authorizeURL, appid, redirect_uri, scope, state);
API 2: 此链接为 API1 中的回调连接, 判断授权是否成功 redirect_uri/?code=CODE&state=STATE OR redirect_uri?state=STATE
var code = ""; // 此code要依据 req.query.code 获取
API 3: 如果成功, 调用获取 access_token 的方法, 由于 secret 问题必须从服务器发起, 也就是说 API2 必须是服务器地址
var accessTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
var url = util.format(accessTokenURL, appid, secret, code);
var ret = {
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE",
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
};
OR
var ret = {"errcode":40029,"errmsg":"invalid code"};
API 4: 刷新 access_token
var refreshTokenURL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
var url = util.format(refreshTokenURL, appid, refreshToken);
var ret = {
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
};
OR
var ret = {"errcode":40029,"errmsg":"invalid code"};
API 5: 拉取用户信息
var loadUserInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
var url = util.format(loadUserInfoURL, accessToken, openid);
var ret = {
"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"
};
OR
var ret = {"errcode":40003,"errmsg":" invalid openid "};