25

I have a simple single-page javascript webapp which uses "Google Sign-In for Websites": https://developers.google.com/identity/sign-in/web/sign-in

How can I get an access token for the user? I need a verifiable assertion of the user's identity on my server. I don't want offline access; I just want to know that when the web client sends an ajax request to my server, I can trust the identity of the logged-in user.

stickfigure
  • 12,641
  • 4
  • 30
  • 46

4 Answers4

60

For verification purposes it would be better to use the id_token which is part of the auth response, and can be retrieved at any point like this:

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().id_token

The Google API Client libraries offer functions to verify the id_token and give you the associated user information on the server side: https://developers.google.com/api-client-library/

Scarygami
  • 12,611
  • 1
  • 32
  • 23
  • 2
    @stickfigure if you are interested, I also wrote an article about server-side access with the new Identity API today, since there were several questions coming in for it and the docs aren't really that extensive about it yet: http://codingwithgerwin.blogspot.co.at/2015/04/google-sign-in-20-server-side.html – Scarygami Apr 21 '15 at 17:31
  • 1
    Great article, I hope it gets indexed quickly :) – stickfigure Apr 21 '15 at 17:43
1

Hope your are doing well. Here is the solution, You may try this: Actually there is no such a function name getAccessToken (Android Only) define in GoogleSignin.android.js as written here https://github.com/devfd/react-native-google-signin. But the best part is they have already implemented the solution in GoogleSignin.android.js. just take a look at the code below from GoogleSignin.android.js

currentUserAsync() {
return new Promise((resolve, reject) => {
  const sucessCb = DeviceEventEmitter.addListener('RNGoogleSignInSilentSuccess', (user) => {
    this._user = user;

    RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    })
    .catch(err => {
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    });
  });

The thing is only we have do use this code wisely. I have use the below code to get access_token and it help me to solve my access token problem. I change above function like this in GoogleSignin.android.js

currentUserAsync() {
return new Promise((resolve, reject) => {
  const sucessCb = DeviceEventEmitter.addListener('RNGoogleSignInSilentSuccess', (user) => {
    this._user = user;

    RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      this._removeListeners(sucessCb, errorCb);
      resolve(token);
    })
    .catch(err => {
      this._removeListeners(sucessCb, errorCb);
      resolve(this._user);
    });
  });

and I call this function like this from index.android.js.

 _signIn() {
    GoogleSignin.signIn()
        .then((user) => {
            console.log('this1' + JSON.stringify(user));
            this.setState({user: user});
           var gettoken =  GoogleSignin.currentUserAsync(user).then((token) => {

                console.log('USER token', token);
                this.setState({user: user});
            }).done();

        }).catch((err) => {
            console.log('WRONG SIGNIN', err);
        })
        .done();
}

You can call it as a individual function it look like this. in GoogleSignin.android.js

getAccessTok(user)
 {
  RNGoogleSignin.getAccessToken(user).then((token) => {
      this._user.accessToken = token;
      resolve(token);
      })
      .catch(err => {
          this._removeListeners(sucessCb, errorCb);
          console.log('got error');
          resolve(this._user);
      });
  }

and from index.android.js just call this function like this

_getToken(){
    console.log(GoogleSignin.getAccessTok(this.state.user));
}

only you have to do is to pass the current user to get access token. Hope this will help you.Have a great day.Thank You.

Pravin Ghorle
  • 330
  • 2
  • 5
0

Yeah, it is 2021. And I was facing the same problem.

My solution is

gapi.signin2.render(this.id, {
    scope: this.scope,
    width: this._width,
    height: this._height,
    longtitle: this._longTitle,
    theme: this.theme,
    // Set to true, otherwise only user actively logging in with Google will have access token 
    onsuccess: (googleUser: gapi.auth2.GoogleUser) => googleUser.getAuthResponse(true),
    onfailure: () => this.handleFailure()
});
tom10271
  • 3,030
  • 4
  • 25
  • 42
0

First, you need to initialize the SDK Then call the following function to get the access-token

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token; 

This solution help for me to get access token in javascript.

Sahil Ralkar
  • 1,428
  • 13
  • 19