Skip to content

Instantly share code, notes, and snippets.

@aritzg
Created July 10, 2013 07:02
Show Gist options
  • Save aritzg/5964046 to your computer and use it in GitHub Desktop.
Save aritzg/5964046 to your computer and use it in GitHub Desktop.
AccountUtil
package net.sareweb.android.lostandfound.util;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import com.google.android.gms.auth.GoogleAuthUtil;
/**
* Created by aritz on 11/06/13.
*/
public class AccountUtil {
private static String token = null;
public static String getGoogleEmail(Context context) {
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType("com.google");
if (accounts != null && accounts.length > 0) {
Account account = (Account) accounts[0];
return account.name;
} else {
return null;
}
}
public static String getGoogleAuthToken(Context context) {
if (token == null) {
try {
token = GoogleAuthUtil.getToken(context, getGoogleEmail(context), "audience:server:client_id:401063832600.apps.googleusercontent.com");
} catch (Exception e) {
e.printStackTrace();
token = null;
}
}
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment