在 Authing 控制台填入 QQ 网页应用信息
上一步用户同意授权之后,会先跳转到 Authing 服务器,之后 Authing 会携带用户信息跳转到开发者在 Authing 控制台中配置的业务回调链接,并附带以下 Get 请求参数:
{
"_id": "5b88aaea349e2d0001a5b718",
"email": "test@test.com",
"emailVerified": false,
"unionid": "123456",
"oauth": "{\"login\":\"123456\",\"id\":123456,\"node_id\":\"MDQ6VXNlcjI0Njk2ODg=\",\"avatar_url\":\"https://avatars3.githubusercontent.com/u/2469688?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/test\",\"html_url\":\"https://github.com/test\",\"followers_url\":\"https://api.github.com/users/test/followers\",\"following_url\":\"https://api.github.com/users/test/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/test/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/test/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/test/subscriptions\",\"organizations_url\":\"https://api.github.com/users/test/orgs\",\"repos_url\":\"https://api.github.com/users/test/repos\",\"events_url\":\"https://api.github.com/users/test/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/test/received_events\",\"type\":\"User\",\"site_admin\":false,\"name\":\"test\",\"company\":\"test\",\"blog\":\"http://test.com\",\"location\":\"Beijing, China\",\"email\":\"test@test.com\",\"hireable\":null,\"bio\":\"Being NO.1\",\"public_repos\":91,\"public_gists\":0,\"followers\":109,\"following\":27,\"created_at\":\"2012-10-02T06:38:50Z\",\"updated_at\":\"2018-07-23T05:51:23Z\"}",
"registerMethod": "oauth:github",
"username": "test",
"nickname": "",
"company": "",
"photo": "https://avatars3.githubusercontent.com/u/2469688?v=4",
"browser": "",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImVtYWlsIjoieGlleWFuZ0Bkb2RvcmEuY24iLCJ1bmlvbmlkIjoiMjQ2OTY4OCIsImlkIjoiNWI4OGFhZWEzNDllMmQwMDAxYTViNzE4IiwiY2xpZW50SWQiOiI1YTlmYTI2Y2Y4NjM1YTAwMDE4NTUyOGMifSwiaWF0IjoxNTU4MTAwMDczLCJleHAiOjE1NTkzOTYwNzN9.7R_-CGnbPBRjHFaVS0ERWMaGfR_24zYJiBTJvJ4XYxk",
"tokenExpiredAt": "Sat Jun 01 2019 21:34:33 GMT+0800 (CST)",
"loginsCount": 47,
"lastLogin": "Fri May 17 2019 21:34:33 GMT+0800 (CST)",
"lastIP": "52.231.14.216",
"signedUp": "Fri Aug 31 2018 10:41:46 GMT+0800 (CST)",
"blocked": false,
"isDeleted": false
}
// 获取 URL 参数
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
// 将 Code 转为 Int 类型,方便判断
const code = parseInt(getQueryString('code'));
if(code !== 200) {
// 出错了
const errorMsg = getQueryString('message');
// 展示 errorMsg 给用户或执行其他业务 ...
}else {
const userInfo = getQueryString('data');
// 将 token 存储到 localStorage
// 建议在之后的请求中附带 Token,并由后端验证 Token 合法性
localStorage.setItem('token', userInfo.token);
}
恭喜你,此时已经接入了 QQ 网页登录。获取到用户信息之后,你可以得到登录凭证 token,你可以在后续的 API 请求中携带上此 token, 然后在后端接口中根据此 token 区分不同用户,详情请见验证 token。