1

how to get access token for google sign

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail().requestIdToken(getString(R.string.server_client_id))
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

in

  GoogleSignInAccount acct = result.getSignInAccount();

no option to get access token

bob
  • 155
  • 3
  • 13
  • http://stackoverflow.com/questions/23759529/android-how-to-get-google-plus-access-token see this one – KCN Jan 14 '16 at 12:54
  • for more visit [Google Play services and OAuth Identity Tools](http://android-developers.blogspot.in/2012/09/google-play-services-and-oauth-identity.html). call `GoogleAuthUtil.getToken(..)` in `onConnected()` callback – Bharatesh Jan 14 '16 at 12:58
  • its for google + as google not removing google + – bob Jan 14 '16 at 13:11
  • in this whi am getting GoogleAuthException – bob Jan 14 '16 at 13:24

2 Answers2

0

According to the answer here you have to request the token from https://www.googleapis.com/oauth2/v4/token

francis
  • 3,852
  • 1
  • 28
  • 30
-4

I just stumbled upon the same problem, solved using this reference: why is requestIdToken returning null?

You can also try this snippet of mine:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("XXXX") //fill this with reference to string value of web client OAuth 2.0 client IDs on https://console.developers.google.com/apis/ 
        .requestEmail() //Remove these below according to your needs
        .requestProfile()
        .requestScopes(new Scope(Scopes.PROFILE))
        .requestScopes(new Scope(Scopes.PLUS_ME))
        .requestScopes(new Scope(Scopes.PLUS_LOGIN))
        .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addApi(Plus.API)
        .build();

And on activity result:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            GoogleSignInAccount acct = result.getSignInAccount();

            if(acct!=null){
                personName = acct.getDisplayName();
                personEmail = acct.getEmail();
                personPhotoUrl = acct.getPhotoUrl().toString();
                String personKey = acct.getIdToken().toString(); //You'll get the token here
}else{

                Toast.makeText(getApplicationContext(),"Login failed!",Toast.LENGTH_LONG).show();
            }
        }
    }

Note: RC_SIGN_IN = 9001;

Community
  • 1
  • 1
Nuge
  • 1
  • you sure this is access token? – neobie Jun 29 '16 at 08:49
  • 1
    @neobie this is called ID Token and this is not access token. – xrnd Jul 18 '16 at 10:19
  • To get access token check this accepted answer http://stackoverflow.com/questions/33998335/how-to-get-access-token-after-user-is-signed-in-from-gmail-in-android. Cheers – xrnd Jul 18 '16 at 11:03
  • @Nuge Id Tokens and access tokens are different. Your implementation give the id token which is basically to verify the returned id for the user – saurabhlahoti Jun 04 '21 at 12:01