Eugene Infinity Eugene Infinity -4 years ago 196
Android Question

Getting Access token from GooglePlus Sign in

I'm currently writing a googleplus sign in app and I'm stuck at getting an access token from google. I've read google plus documentation on getting a access token here. And I'm not sure how to implement it.

below shows part of my code which implements the gplus sign in:

public class GplusAction implements LoginInterface,LogoutInterface{

private GoogleApiClient googleApiClient;
private GoogleSignInOptions googleSignInOptions;
private Context context;
private final static int REQ_CODE = 777;
private Intent signInIntent;
private String accountname;

public GplusAction(Context context) {
this.context = context;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

googleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)

public void login() {
signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult((Activity) context, signInIntent, REQ_CODE, null);

public void logout() {

public void get_onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(signInIntent);

I have tried using GoogleAuthUtils but it seems to have deprecated as described in the link above.

So here are my questions:
- How do I retrieve access token using the new method?

Any ideas?

Answer Source

To get auth token to authenticate with server, you need to follow below steps.

1) Get client id for web application from google console for your app project.

2) Create instance of GoogleApiClient with below GoogleSignInOptions passing above client id.

String serverClientId = getString(R.string.server_oauth_client_id);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(serverClientId )

3) You can retrive id token from GoogleSignInResult after successful login.

GoogleSignInResult result =    Auth.GoogleSignInApi.getSignInResultFromIntent(data);

if (result.isSuccess()) {
    GoogleSignInAccount acct = result.getSignInAccount();
    String idToken = acct.getIdToken();

You can find more details here on implementation of google sign-in option in android app

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download