我目前正在尝试使用Google Signs In for Websites API的 gapi.auth2
,这是我的代码:
- 加载库:
<script src="https://apis.google.com/js/platform.js?onload=onLoadGapiCallback" async defer></script>
- 初始化auth2变量:
var auth2;
window.onLoadGapiCallback = () => {
gapi.load('auth2', () => {
auth2 = gapi.auth2.init({
'client_id': 'CLIENT_ID',
'scope': 'profile email https://www.googleapis.com/auth/youtube.readonly'
});
});
};
- 点击按钮时:
auth2.signIn().then(() => {
console.log('auth is:', auth2.currentUser.get().getAuthResponse().access_token);
});
这很好用,它初始化auth2变量,当我点击按钮时,它显示 SingIn
提示,我选择了一个我的Google帐户 . 问题是从现在开始我必须选择一个YouTube帐户,如果我选择其他帐户而不是主帐户,我会得到一个像这样的异常对象:
{type: "tokenFailed", idpId: "google", error: "USER_LOGGED_OUT"}
还有一个最后发送的XHR请求有这个响应:
{"error":"USER_LOGGED_OUT","detail":"No active session found."}
所以它只适用于我选择 the main account ,但我无法选择其他YouTube帐户 .
我在这里错过了什么?我查看了所有这些文档,但没有人帮助我:
Google Sign-In JavaScript client reference
Monitoring the user's session state
Update :只有在为每个Google帐户选择第一个Youtube帐户时,才能运行此example中的代码(但使用此 scope
: 'profile email https://www.googleapis.com/auth/youtube.readonly'
) . 如果我选择任何其他Youtube帐户,我会收到此警报错误: