简体   繁体   中英

Access Denied Error when using gapi.auth.authorize

I am trying to access a third party's API that uses Google's APIs, but in order to do that, I have to use gapi.auth.authorize to get my Google info to pass to their API. I'm using the new email scope after finding out that userinfo/email is deprecated, but I am still getting an access_denied error.

Here is the boiled down version of what I am having issues with...

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);
}

The console log spits out this error object when it fails...

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: ""

Any insight on what I am doing wrong or where to look next is appreciated. Thanks!

For anyone else having similar issues, I found that if I change the "immediate" parameter in the gap.auth.authorize call to false, it will pop up a box that lets me select a Google account to use. From there, everything runs successfully.

I'm still not 100% on what is happening behind the scenes, but it's working for now. Eventually, I would like to get the immediate authentication working without having to do the pop-up, but that will probably be another question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM