Skip to content

Instantly share code, notes, and snippets.

@Mrono
Created August 14, 2013 02:33
Show Gist options
  • Save Mrono/6227548 to your computer and use it in GitHub Desktop.
Save Mrono/6227548 to your computer and use it in GitHub Desktop.
Get an oauth2 token for an google account
private void getToken(final Context context, final String accountName)
{
final String scope = "oauth2:https://www.googleapis.com/auth/userinfo.profile";
AsyncTask task = new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
final String token;
try {
token = GoogleAuthUtil.getToken(context, accountName, scope);
Log.d(TAG, token);
} catch (GooglePlayServicesAvailabilityException playEx) {
Log.d(TAG, "OMG A CRASH1");
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
(Activity) context,
REQUEST_AUTHORIZATION);
// Use the dialog to present to the user.
} catch (UserRecoverableAuthException recoverableException) {
Log.d(TAG, "OMG A CRASH2");
Intent recoveryIntent = recoverableException.getIntent();
startActivityForResult(recoveryIntent, 2);
// Use the intent in a custom dialog or just startActivityForResult.
} catch (GoogleAuthException authEx) {
// This is likely unrecoverable.
Log.e(TAG, "Unrecoverable authentication exception: " + authEx.getMessage(), authEx);
} catch (IOException ioEx) {
Log.i(TAG, "transient error encountered: " + ioEx.getMessage());
}
return null;
}
};
task.execute((Void) null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment