You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Title</title>
|
|
</head>
|
|
<body>
|
|
<script>
|
|
// 一个同步的http请求
|
|
function httpRequest(address, reqType, asyncProc) {
|
|
var req = new XMLHttpRequest();
|
|
if (asyncProc) {
|
|
req.onreadystatechange = function() {
|
|
if (this.readyState === 4) {
|
|
asyncProc(this);
|
|
}
|
|
};
|
|
}
|
|
req.open(reqType, address, !(!asyncProc));
|
|
req.send();
|
|
return req;
|
|
}
|
|
|
|
// 获取 code 参数
|
|
var query = decodeURI(window.location.search.substring(1));
|
|
var vars = query.split("&");
|
|
var code =''
|
|
for (var i = 0; i < vars.length; i++) {
|
|
var pair = vars[i].split("=");
|
|
if (pair[0] == "code") {
|
|
console.log("code = ",pair[1])
|
|
code=pair[1]
|
|
break
|
|
}
|
|
}
|
|
|
|
// code 换取 access token
|
|
let access_token;
|
|
let token_url = 'http://localhost:9001/oauth2/token?code={Code}&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A9001%2Fcode-to-user-info.html&client_id=juejin';
|
|
token_url =token_url.replace('{Code}',code)
|
|
console.log("token_url = ",token_url)
|
|
const req1 = httpRequest(token_url, "Get", false);
|
|
if ( req1.status == 200) {
|
|
console.log(req1.response )
|
|
}
|
|
let token_data =JSON.parse(req1.response)
|
|
access_token = token_data["access_token"]
|
|
|
|
// access_token 换取用户信息
|
|
let user_info_url = 'http://localhost:9001/oauth2/userinfo?access_token={AccessToken}';
|
|
user_info_url =user_info_url.replace('{AccessToken}',access_token)
|
|
console.log("user_info_url = ",user_info_url)
|
|
|
|
const req2 = httpRequest(user_info_url, "Get", false);
|
|
if ( req2.status === 200) {
|
|
console.log("user info = " ,req2.response )
|
|
}
|
|
|
|
alert( req2.response)
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|
|
|