Skip to content

Instantly share code, notes, and snippets.

View gorankrstevski's full-sized avatar

Goran Krstevski gorankrstevski

View GitHub Profile
@gorankrstevski
gorankrstevski / AuthWithGoogle
Created August 15, 2012 13:04
Authenticate with Google account on Android. Get Access Token valid for User Info Rest service : https://www.googleapis.com/oauth2/v1/userinfo
private String updateToken(boolean invalidateToken) {
String authToken = "null";
try {
AccountManager am = AccountManager.get(getApplicationContext());
Account[] accounts = am.getAccountsByType("com.google");
AccountManagerFuture<Bundle> accountManagerFuture;
if (this == null) {//this is used when calling from an interval thread
accountManagerFuture = am.getAuthToken(accounts[0], "oauth2:https://www.googleapis.com/auth/userinfo.profile" , false, null, null);
} else {
accountManagerFuture = am.getAuthToken(accounts[0], "oauth2:https://www.googleapis.com/auth/userinfo.profile" , null, this, null, null);