3

我正在尝试访问使用 Google API 的第三方 API,但为了做到这一点,我必须使用 gapi.auth.authorize 将我的 Google 信息传递给他们的 API。在发现 userinfo/email 已弃用后,我正在使用新的电子邮件范围,但我仍然收到 access_denied 错误。

这是我遇到的问题的简化版本...

索引.html

<!doctype html>
<html>
<head>
</head>
<body>
   <p>Tripping all day...</p>
   <p id="output"></p>
   <script src="auth.js"></script>

   <script type="text/javascript">
       function init() {
       console.log('init');
       checkAuth();
       }
   </script>
   <script src="https://apis.google.com/js/client.js?onload=init">    </script>
   <script>
      document.getElementById("output").innerHTML = "Coooooorrrrrraaaaalll";
   </script>
`enter code here`</body>
</html>

auth.js

CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';

var SCOPES = 'email';

function handleAuth(authResult) {
    console.log('handle auth');
    console.log(authResult);
}

function checkAuth() {
    console.log('check auth');
    gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: true, cookie_policy: 'single_host_origin'}, handleAuth);
}

控制台日志在失败时会吐出此错误对象...

client_id: "xxxxxxxxxxxxx.apps.googleusercontent.com"
cookie_policy: "single_host_origin"
error: "immediate_failed"
error_subtype: "access_denied"
expires_at: "1438103114"
expires_in: "86400"
g_user_cookie_policy: "single_host_origin"
issued_at: "1438016714"
response_type: "token"
scope: "email"
state: ""

任何关于我做错了什么或下一步看哪里的见解都值得赞赏。谢谢!

4

1 回答 1

3

对于其他有类似问题的人,我发现如果我将 gap.auth.authorize 调用中的“立即”参数更改为 false,它会弹出一个框,让我选择要使用的 Google 帐户。从那里开始,一切顺利。

我仍然不是 100% 了解幕后发生的事情,但它现在正在工作。最终,我希望无需弹出窗口即可立即进行身份验证,但这可能是另一个问题。

于 2015-07-28T20:29:45.133 回答