使用gapi.auth.authorize时访问被拒绝错误

时间:2015-07-27 17:23:20

标签: javascript google-api google-oauth gapi

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

以下是我遇到问题的简要版本......

的index.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: ""

对于我做错了什么或下一步看什么的任何见解都表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:3)

对于其他有类似问题的人,我发现如果我改变&#34;立即&#34; gap.auth.authorize中的参数调用为false,它会弹出一个框,让我选择要使用的Google帐户。从那里,一切都成功运行。

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