我一直在阅读文档,到目前为止还没有成功,还需要 OAuth 访问令牌。但是,Google Identity 服务不会返还 oAuth 访问令牌。相反,它返回一个 JWT 令牌。
我正在寻找使用一键登录的 JWT 令牌响应进行传递的方法,以便我可以取回 oAuth 访问令牌。
文档链接: Google 一键登录
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
window.onload = function () {
google.accounts.id.initialize({
client_id: 'myid.apps.googleusercontent.com',
callback: handleCredentialResponse
});
google.accounts.id.prompt();
}
</script>
<script>
function parseJwt(token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};
function handleCredentialResponse(response) {
console.log(response);
const responsePayload = parseJwt(response.credential);
console.log(responsePayload);
}
</script>
Run Code Online (Sandbox Code Playgroud)