Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ API doesnt return access_token Javascript

We have an application that relies upon Google to authenticate its users against our google apps account and then do some serverside verification and group lookups.

Recently google changed the name of the object that held the access_token variable which we require to authenticate. In the docs (https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile) it says that access_token is available from the getAuthResponse() method, however when i use this it comes back as undefined. Inspecting the object after console.log() reveals all the other fields mentioned except access_token. I'm worried that Google will change the object again in the future and leave us without our application. Here is the code.

<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<meta name="google-signin-client_id" content="XXX.apps.googleusercontent.com">
<script>
    //This happens after the user has authenticated with Google and has been passed
    //back to the page
        function onSignIn(googleUser) {
            //Check to see whether the user is trying to sign out.
            if (window.location.href.indexOf("signOut=1") !== -1) {
                //Sign them out of the application.
                signOut();
                //redirect them to the same page, without the signOut query string so they can log back in if want
                window.location.href='googlesigninform.html'
                return false;
            }
            //Grab the token, access token and email.
            var _id = googleUser.getAuthResponse().id_token; //This works
            var _accessToken = googleUser.Ka.access_token; //This works but changed from googleUser.B.access_token
            var profile = googleUser.getBasicProfile(); //Works
            console.log(googleUser.access_token); //Undefined
            console.log(googleUser.getAuthResponse().access_token);//Undefined
            //Make a post request to the API
            makePostRequest(_id, _accessToken, profile.getEmail());
        }

What is the correct way to access the access_token variable?

like image 546
Andrew MacNaughton Avatar asked Sep 10 '15 17:09

Andrew MacNaughton


People also ask

How do I refresh my Google Drive token?

Go to https://developers.google.com/oauthplayground. Make sure you added this URL to your Authorized redirect URIs in the previous step. In the top right corner, click the settings icon, check "Use your own OAuth credentials" and paste your Client ID and Client Secret.

How does OAuth 2.0 work in REST API?

Using OAuth 2.0, it is possible for the application to access the user's data without the disclosure of the user's credentials to the application. The API will grant access only when it receives a valid access token from the application.

How do I get the access token from refresh token?

To get a refresh token , you must include the offline_access scope when you initiate an authentication request through the /authorize endpoint. Be sure to initiate Offline Access in your API.


1 Answers

If you need to use access token you are using the wrong type of google signin flow. You should follow this page: https://developers.google.com/identity/sign-in/web/server-side-flow

What you did implement is google Sign-In to identify users (https://developers.google.com/identity/sign-in/web/)

Which only provides a unique id per user because it is meant to authenticate the user for your own service and not to give an access token to use for other Google services later on.

like image 123
Bas van Stein Avatar answered Sep 22 '22 00:09

Bas van Stein