Created
August 15, 2012 13:04
-
-
Save gorankrstevski/3359938 to your computer and use it in GitHub Desktop.
Authenticate with Google account on Android. Get Access Token valid for User Info Rest service : https://www.googleapis.com/oauth2/v1/userinfo
This file contains hidden or 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 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); | |
} | |
Bundle authTokenBundle = accountManagerFuture.getResult(); | |
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString(); | |
if (invalidateToken) { | |
am.invalidateAuthToken("com.google", authToken); | |
authToken = updateToken(false); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return authToken; | |
} |
This file contains hidden or 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
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> | |
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> | |
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> | |
<uses-permission android:name="android.permission.INTERNET" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment