Example usage for android.accounts AccountManager KEY_AUTHTOKEN

List of usage examples for android.accounts AccountManager KEY_AUTHTOKEN

Introduction

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

Prototype

String KEY_AUTHTOKEN

To view the source code for android.accounts AccountManager KEY_AUTHTOKEN.

Click Source Link

Document

Bundle key used for the auth token value in results from #getAuthToken and friends.

Usage

From source file:com.nononsenseapps.notepad.MainActivity.java

private void enableSync() {
    final Activity activity = this;
    // Get the first Google account on the device
    final Account[] accounts = AccountManager.get(activity).getAccountsByType("com.google");
    if (accounts.length > 0) {
        final Account account = accounts[0];

        // Request access
        AccountManager.get(activity).getAuthToken(account, SyncAdapter.AUTH_TOKEN_TYPE, null, activity,
                new AccountManagerCallback<Bundle>() {

                    @Override//from w  w  w .java 2  s.c  o  m
                    public void run(AccountManagerFuture<Bundle> future) {
                        // This is the callback class, it handles all the
                        // steps
                        // after requesting access
                        try {
                            String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                            if (token != null && !token.equals("") && account != null) {
                                // Get preference editor
                                Editor editor = PreferenceManager.getDefaultSharedPreferences(activity).edit();

                                // Write account name to prefs
                                editor.putString(SyncPrefs.KEY_ACCOUNT, account.name);

                                // Set it syncable
                                ContentResolver.setIsSyncable(account, NotePad.AUTHORITY, 1);

                                // Write to pref
                                editor.putBoolean(SyncPrefs.KEY_SYNC_ENABLE, true);

                                // Commit prefs
                                editor.commit();

                                // Then request sync
                                requestSync(account.name);
                            }
                        } catch (OperationCanceledException e) {
                            Log.e("SyncFix", "Error1");
                            // if the request was canceled for any reason
                        } catch (AuthenticatorException e) {
                            Log.e("SyncFix", "Error2");
                            // if there was an error communicating with the
                            // authenticator or
                            // if the authenticator returned an invalid
                            // response
                        } catch (IOException e) {
                            Log.e("SyncFix", "Error3");
                            // if the authenticator returned an error
                            // response that
                            // indicates that it encountered an IOException
                            // while
                            // communicating with the authentication server
                        }
                    }
                }, null);

    }
}

From source file:com.owncloud.android.authentication.AuthenticatorActivity.java

/**
 * Sets the proper response to get that the Account Authenticator that
 * started this activity saves a new authorization token for mAccount.
 *///from www  .  ja v  a 2 s  .  c  om
private boolean updateToken() {
    Bundle response = new Bundle();
    response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
    response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);

    if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mAuthTokenType)) {
        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling
        // directly to the AuthenticatorActivity to update, without
        // AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mAuthTokenType)) {
        String username = getUserNameForSamlSso();

        if (!(mUsernameInput.getText().toString() + "@" + location).equals(username)) {
            // fail - not a new account, but an existing one; disallow
            RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME);
            updateAuthStatusIconAndText(result);
            showAuthStatus();
            Log_OC.d(TAG, result.getLogMessage());

            return false;
        }

        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling
        // directly to the AuthenticatorActivity to update, without
        // AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else {
        response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());
        mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());
    }
    setAccountAuthenticatorResult(response);

    return true;
}

From source file:com.digitalarx.android.authentication.AuthenticatorActivity.java

/**
 * Sets the proper response to get that the Account Authenticator that started this activity saves 
 * a new authorization token for mAccount.
 *///from w ww  .j a  v a  2s  .c o m
private void updateToken() {
    Bundle response = new Bundle();
    response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
    response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);

    if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType())
            .equals(mAuthTokenType)) {

        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else {
        response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());
        mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());
    }
    setAccountAuthenticatorResult(response);

}

From source file:com.cerema.cloud2.authentication.AuthenticatorActivity.java

/**
 * Updates the authentication token./*from  w ww  .j  ava2s  .c o  m*/
 *
 * Sets the proper response so that the AccountAuthenticator that started this activity
 * saves a new authorization token for mAccount.
 *
 * Kills the session kept by OwnCloudClientManager so that a new one will created with
 * the new credentials when needed.
 */
private void updateAccountAuthentication() throws AccountNotFoundException {

    Bundle response = new Bundle();
    response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
    response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);

    if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary, notifications are calling directly to the 
        // AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType())
            .equals(mAuthTokenType)) {

        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling directly to the 
        // AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);

    } else {
        response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());
        mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());
    }
    setAccountAuthenticatorResult(response);

}