📖 本教程更新于 2021 年 10 月 09 日

微信开放平台测试号系统

公众号测试平台
网页授权文档
UrlEncode 编码/解码

使用

1-用户同意授权,获取 code

1
2
3
4
5
6
7
8
9
let appid = "";
let uri = ""; // urlEncode 对链接进行处理
window.open(
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
appid +
"&redirect_uri=" +
url +
"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
);

2-通过 code 换取网页授权 access_token

1
2
3
4
5
6
7
8
9
10
11
12
let appid = "";
let appsecret = "";
let code = "";
window.open(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
appid +
"&secret=" +
appsecret +
"&code=" +
code +
"&grant_type=authorization_code"
);

返回如下

1
2
3
4
5
6
7
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}

3-拉取用户信息

1
2
3
4
5
6
7
8
9
let access_token = "";
let openid = "";
window.open(
" https://api.weixin.qq.com/sns/userinfo?access_token=" +
access_token +
"&openid=" +
openid +
"&lang=zh_CN"
);

返回如下

1
2
3
4
5
6
7
8
9
10
11
{
"openid": "OPENID",
"nickname": NICKNAME,
"sex": 1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY",
"headimgurl":"https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}