我目前正在尝试使用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."}
所以只有当我选择主要帐户时才会有效,但我无法选择其他YouTube帐户。
我在这里缺少什么? 我已经查看了所有这些文档,但没有人帮助过我:
Google Sign-In JavaScript client reference
Monitoring the user's session state
更新:
只有当我为每个Google帐户选择第一个Youtube帐户时,才能运行此example中的代码(但使用此scope
:'profile email https://www.googleapis.com/auth/youtube.readonly'
)。如果我选择任何其他Youtube帐户,我将收到此警告错误: