【问题标题】:gapi.auth javascript commands are not executed properlygapi.auth javascript 命令未正确执行
【发布时间】:2014-12-27 04:01:24
【问题描述】:

我正在尝试遵循 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 文件时 - none 的 2 个 'HandleAuthResult' 可选警报显示在屏幕上at all - 这意味着此功能不被“gapi.auth.authorize”回调

有人设法正确使用这个库吗?

【问题讨论】:

    标签: javascript oauth oauth-2.0 google-api google-api-client


    【解决方案1】:

    首先,您需要检查 chrome 是否阻止了您的弹出窗口。这是因为弹出式打开代码不在点击处理程序中。

    然后您应该打开 chrome 的开发者控制台并检查“控制台”选项卡是否有错误。

    您还应该在 Google Developer Console 中指定您的域名和页面所在的端口,如下所示:

    我能够登录谷歌并收到成功的回调 使用您的代码,但仅在删除“立即:真”选项之后。 我不知道为什么,但我收到“immediate_failed”错误。

    【讨论】:

    • 谢谢。这很有帮助。此外,它仅在我从网络服务器(url 以 http 开头)而不是直接通过文件系统运行 html 时才有效。
    猜你喜欢
    • 2021-12-04
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2021-01-18
    • 1970-01-01
    • 2023-01-17
    • 2014-06-13
    相关资源
    最近更新 更多