Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A quick example of retrieving an access token with GoogleAuthUtil
package com.google.devrel.samples.gmstest.app;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.plus.Account;
import com.google.android.gms.plus.Plus;
import java.io.IOException;
public class RetrieveAccessTokenActivity extends Activity implements View.OnClickListener {
private static final String TAG = "RetrieveAccessToken";
private static final int REQ_SIGN_IN_REQUIRED = 55664;
private String mAccountName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_access_token);
findViewById(R.id.button_token).setOnClickListener(this);
// Manual integration? Pop an account chooser to get this:
mAccountName = "me@example.com";
// Or if you have a GoogleApiClient connected:
// mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button_token) {
new RetrieveTokenTask().execute(mAccountName);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_SIGN_IN_REQUIRED && resultCode == RESULT_OK) {
// We had to sign in - now we can finish off the token request.
new RetrieveTokenTask().execute(mAccountName);
}
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
String scopes = "oauth2:profile email";
String token = null;
try {
token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
((TextView) findViewById(R.id.token_value)).setText("Token Value: " + s);
}
}
}
@c0d3rm0nk3y

This comment has been minimized.

Copy link

@c0d3rm0nk3y c0d3rm0nk3y commented Nov 16, 2014

thank you very much. I still haven't learned the finer aspects of Google's Dev doc's and spent all day trying to get their tutorials to run. Then I came across this example, 5 min and boom I got exactly what I need. Thank you again.

@agamemnus

This comment has been minimized.

Copy link

@agamemnus agamemnus commented Dec 2, 2014

String scopes = "oauth2:profile email";

Does this well truly, honest-to-god work properly? Everything I have attempted has shown that it does not work reliably. Sometimes it works, sometimes it doesn't. It's driving me insane. Could it be something to do with my GoogleApiClient.Builder object?

Sometimes, I get two permissions screens: one for the whole mess of Google Plus permissions (circles, etcetc), and then the "basic" permission right after. Sometimes, it's just the first one, and then the running the authentication token gives me "invalid permissions", etc.

http://stackoverflow.com/questions/27239405/googleauthutil-get-access-token-that-will-provide-an-email-address

@thedumbtechguy

This comment has been minimized.

Copy link

@thedumbtechguy thedumbtechguy commented Mar 18, 2015

That really helped! Thanks a bunch

@thedumbtechguy

This comment has been minimized.

Copy link

@thedumbtechguy thedumbtechguy commented Mar 18, 2015

@agamemnus the reason you get two permission screens is due to this catch block

catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
}

if the app doesn't have the necessary permissions, this will launch a new screen. You need to handle the new connection and attempt to retrieve the token again if the second screen opens up.

@manchikantishyam

This comment has been minimized.

Copy link

@manchikantishyam manchikantishyam commented Aug 6, 2015

Thanks a Ton. It saved lot of time.

@yurevich1

This comment has been minimized.

Copy link

@yurevich1 yurevich1 commented Feb 3, 2016

Due to this site is in the top of Google search ("GoogleApiClient get token") I wanna write this methods are deprecated!
Do not waste your time to use this code (Feb 2016).

@X-Strange

This comment has been minimized.

Copy link

@X-Strange X-Strange commented Mar 4, 2016

yurevich1, thanks for the warning, but which is right method then? I'm searching for a complete solution.

@hmrocket

This comment has been minimized.

Copy link

@hmrocket hmrocket commented Oct 28, 2016

@X-Strange did someone found the new to retrieve the access toke, cause getToken() is deprecated

@KCKH

This comment has been minimized.

Copy link

@KCKH KCKH commented Nov 25, 2016

@RanaRanvijaySingh

This comment has been minimized.

Copy link

@RanaRanvijaySingh RanaRanvijaySingh commented Jul 15, 2017

Worked like a charm ... thank you so much.

@sivaramakrishnant

This comment has been minimized.

Copy link

@sivaramakrishnant sivaramakrishnant commented Jul 31, 2017

Worked.But with that Access token i could not be able to receive the Contact list .
Please give me solution if you can.
Thanks in advance

@dinesh555

This comment has been minimized.

Copy link

@dinesh555 dinesh555 commented Sep 13, 2018

@ianbarber thank you very much.That really helped #

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.