List of usage examples for android.accounts AccountManager getAuthToken
public AccountManagerFuture<Bundle> getAuthToken(final Account account, final String authTokenType, final Bundle options, final boolean notifyAuthFailure, AccountManagerCallback<Bundle> callback, Handler handler)
From source file:cn.code.notes.gtask.remote.GTaskClient.java
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken;//from w w w. java 2s. c o m AccountManager accountManager = AccountManager.get(activity); Account[] accounts = accountManager.getAccountsByType("com.google"); if (accounts.length == 0) { Log.e(TAG, "there is no available google account"); return null; } String accountName = SystemEditActivity.getSyncAccountName(activity); Account account = null; for (Account a : accounts) { if (a.name.equals(accountName)) { account = a; break; } } if (account != null) { mAccount = account; } else { Log.e(TAG, "unable to get an account with the same name in the settings"); return null; } // get the token now AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile", null, activity, null, null); try { Bundle authTokenBundle = accountManagerFuture.getResult(); authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); if (invalidateToken) { accountManager.invalidateAuthToken("com.google", authToken); loginGoogleAccount(activity, false); } } catch (Exception e) { Log.e(TAG, "get auth token failed"); authToken = null; } return authToken; }
From source file:com.example.simba.myapplicationnote.gtask.remote.GTaskClient.java
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken;// w w w . j a va 2s .c o m AccountManager accountManager = AccountManager.get(activity); Account[] accounts = accountManager.getAccountsByType("com.google"); if (accounts.length == 0) { Log.e(TAG, "there is no available google account"); return null; } String accountName = NotesPreferenceActivity.getSyncAccountName(activity); Account account = null; for (Account a : accounts) { if (a.name.equals(accountName)) { account = a; break; } } if (account != null) { mAccount = account; } else { Log.e(TAG, "unable to get an account with the same name in the settings"); return null; } // get the token now AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile", null, activity, null, null); try { Bundle authTokenBundle = accountManagerFuture.getResult(); authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); if (invalidateToken) { accountManager.invalidateAuthToken("com.google", authToken); loginGoogleAccount(activity, false); } } catch (Exception e) { Log.e(TAG, "get auth token failed"); authToken = null; } return authToken; }
From source file:falcofinder.android.fuehrerschein.chat.AccountsActivity.java
/** * Registers for C2DM messaging with the given account name. * /*from ww w. j a va 2 s. co m*/ * @param accountName a String containing a Google account name */ private void register(final String accountName) { // Store the account name in shared preferences final SharedPreferences prefs = Util.getSharedPreferences(mContext); SharedPreferences.Editor editor = prefs.edit(); editor.putString(Util.ACCOUNT_NAME, accountName); editor.remove(Util.AUTH_COOKIE); editor.remove(Util.DEVICE_REGISTRATION_ID); editor.commit(); // Obtain an auth token and register final AccountManager mgr = AccountManager.get(mContext); Account[] accts = mgr.getAccountsByType("com.google"); for (Account acct : accts) { final Account account = acct; if (account.name.equals(accountName)) { if (Util.isDebug(mContext)) { // Use a fake cookie for the dev mode app engine server // The cookie has the form email:isAdmin:userId // We set the userId to be the same as the email String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName; prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit(); //commento c2dm non usato //C2DMessaging.register(mContext, Setup.SENDER_ID); } else { // Get the auth token from the AccountManager and convert // it into a cookie for the appengine server final Activity activity = this; mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() { public void run(AccountManagerFuture<Bundle> future) { String authToken = getAuthToken(future); // Ensure the token is not expired by invalidating it and // obtaining a new one mgr.invalidateAuthToken(account.type, authToken); mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() { public void run(AccountManagerFuture<Bundle> future) { String authToken = getAuthToken(future); // Convert the token into a cookie for future use String authCookie = getAuthCookie(authToken); Editor editor = prefs.edit(); editor.putString(Util.AUTH_COOKIE, authCookie); editor.commit(); //commento c2dm non usato // C2DMessaging.register(mContext, Setup.SENDER_ID); } }, null); } }, null); } break; } } }
From source file:com.microsoft.windowsazure.mobileservices.MobileServiceClient.java
/** * Invokes Microsoft Azure Mobile Service authentication using a the Google * account registered in the device//from w w w .j a va 2s.c o m * * @param activity The activity that triggered the authentication * @param account The account used for the login operation * @param scopes The scopes used as authentication token type for login */ public ListenableFuture<MobileServiceUser> loginWithGoogleAccount(Activity activity, Account account, String scopes) { final SettableFuture<MobileServiceUser> future = SettableFuture.create(); try { if (account == null) { throw new IllegalArgumentException("account"); } final MobileServiceClient client = this; AccountManagerCallback<Bundle> authCallback = new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> futureBundle) { try { if (futureBundle.isCancelled()) { future.setException(new MobileServiceException("User cancelled")); // callback.onCompleted(null, new // MobileServiceException("User cancelled"), null); } else { Bundle bundle = futureBundle.getResult(); String token = (String) (bundle.get(AccountManager.KEY_AUTHTOKEN)); JsonObject json = new JsonObject(); json.addProperty("access_token", token); ListenableFuture<MobileServiceUser> loginFuture = client .login(MobileServiceAuthenticationProvider.Google, json); Futures.addCallback(loginFuture, new FutureCallback<MobileServiceUser>() { @Override public void onFailure(Throwable e) { future.setException(e); } @Override public void onSuccess(MobileServiceUser user) { future.set(user); } }); } } catch (Exception e) { future.setException(e); } } }; AccountManager acMgr = AccountManager.get(activity.getApplicationContext()); acMgr.getAuthToken(account, scopes, null, activity, authCallback, null); } catch (Exception e) { future.setException(e); } return future; }