Example usage for android.accounts AccountManager getAuthToken

List of usage examples for android.accounts AccountManager getAuthToken

Introduction

In this page you can find the example usage for android.accounts AccountManager getAuthToken.

Prototype

public AccountManagerFuture<Bundle> getAuthToken(final Account account, final String authTokenType,
        final Bundle options, final boolean notifyAuthFailure, AccountManagerCallback<Bundle> callback,
        Handler handler) 

Source Link

Document

Gets an auth token of the specified type for a particular account, optionally raising a notification if the user must enter credentials.

Usage

From source file:com.google.sampling.experiential.android.lib.GoogleAccountLoginHelper.java

private String getNewAuthToken(AccountManager accountManager, Account account)
        throws OperationCanceledException, IOException, AuthenticatorException {
    AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "ah", null,
            context, null, null);//  w  w w  .ja v a  2  s .c o  m

    Bundle authTokenBundle = accountManagerFuture.getResult();
    return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
}

From source file:org.pixmob.feedme.ui.EntriesFragment.java

private void authenticateAccount(String accountName) {
    Log.i(TAG, "Authenticating account: " + accountName);

    final Activity a = getActivity();
    final AccountManager am = (AccountManager) a.getSystemService(Context.ACCOUNT_SERVICE);
    final Account account = new Account(accountName, GOOGLE_ACCOUNT);
    am.getAuthToken(account, "reader", null, a, new AccountManagerCallback<Bundle>() {
        @Override//from  w ww .  jav a2s .  c o m
        public void run(AccountManagerFuture<Bundle> resultContainer) {
            String authToken = null;
            final Bundle result;
            try {
                result = resultContainer.getResult();
                authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
            } catch (IOException e) {
                Log.w(TAG, "I/O error while authenticating account " + account.name, e);
            } catch (OperationCanceledException e) {
                Log.w(TAG, "Authentication was canceled for account " + account.name, e);
            } catch (AuthenticatorException e) {
                Log.w(TAG, "Authentication failed for account " + account.name, e);
            }

            if (authToken == null) {
                Toast.makeText(a, a.getString(R.string.authentication_failed), Toast.LENGTH_SHORT).show();
            } else {
                Log.i(TAG, "Authentication done");
                onAuthenticationDone(account.name, authToken);
            }
        }
    }, null);
}

From source file:com.github.opengarageapp.activity.MainActivity.java

public void getAuthAndDo(String intent) {
    Account account = application.getSelectedAccount();
    AccountManager manager = AccountManager.get(this);
    manager.getAuthToken(account, "oauth2:https://www.googleapis.com/auth/userinfo.email", new Bundle(), this,
            new GarageActionAuthCallback(intent), null);
}

From source file:com.android.browser.GoogleAccountLogin.java

private void invalidateTokens() {
    AccountManager am = AccountManager.get(mActivity);
    am.invalidateAuthToken(GOOGLE, mSid);
    am.invalidateAuthToken(GOOGLE, mLsid);
    mTokensInvalidated = true;//  w  w  w . j  av a 2 s  .  c  om
    mState = 1; // SID
    am.getAuthToken(mAccount, "SID", null, mActivity, this, null);
}

From source file:com.noswap.keyring.MainActivity.java

public void doGCMStuff() {
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        Log.v(TAG, "Registering");
        GCMRegistrar.register(this, SENDER_ID);
    } else {//  w  w w .  ja  v a2  s.c om
        Log.v(TAG, "Already registered: " + regId);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType("com.google");
    for (Account account : accounts) {
        Log.v(TAG, "Account: " + account.name + " (" + account.type + ")");
    }

    if (accounts.length > 0) {
        Account account = accounts[0];
        Bundle options = new Bundle();
        am.getAuthToken(account, "Keyring", options, this, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> result) {
                try {
                    Bundle bundle = result.getResult();
                    String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                    if (intent != null) {
                        startActivityForResult(intent, 0);
                        return;
                    }
                    Log.v(TAG, "onTokenAcquired: " + token);
                } catch (Exception e) {
                    Log.v(TAG, "onTokenAcquired exception: " + e.toString());
                }
            }
        }, new Handler() {
        });

    }
}

From source file:com.arthackday.killerapp.util.Util.java

public String getGoogleAuth(String type) {
    AccountManager mgr = AccountManager.get(activity);
    Account[] accts = mgr.getAccountsByType("com.google");

    if (accts.length == 0) {
        return null;
    }//from  ww  w.j ava  2 s . c  om

    try {
        Account acct = accts[0];
        Log.d(LOG_TAG, "acct name=" + acct.name);
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, type, null, activity, null,
                null);

        Bundle authTokenBundle = accountManagerFuture.getResult();

        if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
            Intent authRequestIntent = (Intent) authTokenBundle.get(AccountManager.KEY_INTENT);
            activity.startActivity(authRequestIntent);
        }

        return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String promote(final Activity activity, String inAuthority, final String token) {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    //      Bundle options = new Bundle();
    //      if (token != null) {
    //         options.putString(Constants.PROMOTION_TOKEN, token);
    //      }//from   w w  w  .  ja va2s  . c om
    final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
    final String userDataString = am.getUserData(a, AccountManager.KEY_USERDATA);

    invalidateToken(activity, authority);

    am.getAuthToken(new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)),
            authority, null, null, new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle = null;
                    try {
                        bundle = result.getResult();
                        Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.putExtra(Constants.KEY_AUTHORITY, authority);
                            launch.putExtra(Constants.PROMOTION_TOKEN, token);
                            launch.putExtra(Constants.OLD_DATA, userDataString);
                            activity.startActivityForResult(launch, SC_AUTH_ACTIVITY_REQUEST_CODE);
                        } else if (bundle.getString(AccountManager.KEY_AUTHTOKEN) != null) {
                            //                         am.setAuthToken(a, authority, bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            //                         am.addAccountExplicitly(a, null, null);
                            // no token acquired
                        } else {
                            storeAnonymousToken(token, authority, am, a);
                        }
                    } catch (Exception e) {
                        // revert the invalidated token
                        storeAnonymousToken(token, authority, am, a);
                        return;
                    }
                }
            }, null);
    return null;
}

From source file:org.brussels.gtug.attendance.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from  ww  w  . ja  va2  s  . c o 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)) {
            // 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();
                            C2DMessaging.register(mContext, Constants.SENDER_ID);
                        }
                    }, null);
                }
            }, null);
            break;
        }
    }
}

From source file:com.murrayc.galaxyzoo.app.LoginUtils.java

/**
 * This returns null if there is no account (not even an anonymous account).
 * Don't call this from the main thread - use an AsyncTask, for instance.
 *
 * @param context/*from w  w w  .j av  a  2s .c o  m*/
 * @return
 */
@Nullable
public static LoginDetails getAccountLoginDetails(final Context context) {
    final AccountManager mgr = AccountManager.get(context);
    if (mgr == null) {
        Log.error(
                "getAccountLoginDetails(): getAccountLoginDetails() failed because AccountManager.get() returned null.");
        return null;
    }

    final Account account = getAccount(mgr);
    if (account == null) {
        Log.error(
                "getAccountLoginDetails(): getAccountLoginDetails() failed because getAccount() returned null. ");
        return null;
    }

    //Make sure that this has not been unset somehow:
    setAutomaticAccountSync(context, account);

    final LoginDetails result = new LoginDetails();

    //Avoid showing our anonymous account name in the UI.
    //Also, an anonymous account never has an auth_api_key.
    result.isAnonymous = TextUtils.equals(account.name, ACCOUNT_NAME_ANONYMOUS);
    if (result.isAnonymous) {
        return result; //Return a mostly-empty empty (but not null) LoginDetails.
    }

    result.name = account.name;

    //Note that this requires the USE_CREDENTIALS permission on
    //SDK <=22.
    final AccountManagerFuture<Bundle> response = mgr.getAuthToken(account, ACCOUNT_AUTHTOKEN_TYPE, null, null,
            null, null);
    try {
        final Bundle bundle = response.getResult();
        if (bundle == null) {
            //TODO: Let the caller catch this?
            Log.error(
                    "getAccountLoginDetails(): getAccountLoginDetails() failed because getAuthToken() returned a null response result bundle.");
            return null;
        }

        result.authApiKey = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        return result;
    } catch (final OperationCanceledException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    } catch (final AuthenticatorException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    } catch (final IOException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    }
}

From source file:com.nest5.businessClient.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from  w ww  .jav  a2  s.c o 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)) {

            // 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();
                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        }
                    }, null);
                }
            }, null);

            break;
        }
    }
}