Created
August 14, 2013 02:33
-
-
Save Mrono/6227548 to your computer and use it in GitHub Desktop.
Get an oauth2 token for an google account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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