我正在尝试遵循 Google's developer tutorial 中的示例
使用更新的 chrome(版本 38.0)
但似乎 gapi.auth 函数从未达到其回调
这是一个演示它的代码示例:
<!doctype html>
<html>
<head>
<title>Google API Client test</title>
</head>
<body>
here will be the use of Oauth 2.0
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
googleApiClientReady = function() {
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({
client_id: 'XXX',
scope: 'https://www.googleapis.com/auth/youtube',
immediate: true
}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
alert('gapi.auth return successfully');
} else {
alert('gapi.auth return with error');
}
}
</script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
</html>
当我运行上面的 html+JS 文件时-屏幕上根本没有显示“HandleAuthResult”的 2 个可选警报中的无 - 这意味着此功能不是由“gapi.auth.authorize”回调
有没有人设法正确使用这个库?
最佳答案
首先,您需要检查 chrome 是否阻止了您的弹出窗口。这是因为弹出窗口打开代码不在单击处理程序中。
然后你应该打开 chrome 的开发者控制台并检查“控制台”选项卡是否有错误。
您还应该在 Google Developer Console 中指定您的网页所在的域名和端口,如下所示:
我能够登录到谷歌并收到成功的回调 使用您的代码,但仅在删除“立即:真实”选项之后。 我不知道为什么,但我收到了“immediate_failed”错误。
关于javascript - gapi.auth javascript 命令未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26664407/