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:Main.java

public static String getUserToken(Context context, Account account) {
    AccountManager manager = AccountManager.get(context);
    return manager.getUserData(account, AccountManager.KEY_AUTHTOKEN);
}

From source file:Main.java

public static String refreshAuthToken(Activity activity, String token, String name, String googleApi) {
    String authToken = null;/*from  www . j  a v a  2s .c o m*/
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    try {
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, token);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:Main.java

public static String getAuthToken(Activity activity, String name, String googleApi) {
    String authToken = null;/*  w  w  w .ja  v a 2  s  .c o  m*/
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null, null);

    try {
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

/**
 * Attempts to authenticate the user using a pre-existing stored authentication token.  If an account exists, but no such token 
 * exists, then the user will be prompted by the account authenticator to re-enter their Google credentials to generate the new token.
 * //  w ww .  j  a v a2  s . co  m
 * @param act the calling activity
 * @return the authentication token for the requested service or null if there is no Google Account.
 * @throws AuthenticatorException if an error occurs during authentication.
 * @throws OperationCanceledException
 * @throws IOException
 */
public static String authenticate(Activity act, String serviceCode)
        throws AuthenticatorException, OperationCanceledException, IOException {
    AccountManager mgr = AccountManager.get(act);
    Account[] accts = mgr.getAccountsByType(GCLAccountAuthenticator.ACCT_TYPE);
    if (accts.length > 0) {
        Account acct = accts[0];
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, serviceCode, null, act, null,
                null);
        Bundle authTokenBundle = accountManagerFuture.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        return authToken;
    } else {
        Log.e(TAG, "No google accounts registered for this device.");
        return null;
    }
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.ui.settings.ActivitySettings.java

/**
 * Called when the user has selected a Google account when pressing the enable Gtask switch.
 *//*  www. j  a v  a  2  s. c  o  m*/
@SuppressLint("CommitPrefEdits")
@Override
public void run(AccountManagerFuture<Bundle> future) {
    try {
        // If the user has authorized
        // your application to use the
        // tasks API
        // a token is available.
        String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
        // Now we are authorized by the user.

        if (token != null && !token.isEmpty()) {
            // Also mark enabled as true, as the dialog was shown from enable button
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            sharedPreferences.edit().putBoolean(getString(R.string.const_preference_gtask_enabled_key), true)
                    .commit();

            // Set it syncable
            SyncGtaskHelper.toggleSync(this, sharedPreferences);
            // And schedule an immediate sync
            SyncGtaskHelper.requestSyncIf(this, SyncGtaskHelper.MANUAL);
        }
    } catch (OperationCanceledException | AuthenticatorException | IOException ignored) {
        // if the request was canceled for any reason, or something went wrong
        SyncGtaskHelper.disableSync(this);
    }
}

From source file:com.android.volley.toolbox.AndroidAuthenticatorTest.java

@Test
public void goodToken() throws Exception {
    Bundle bundle = new Bundle();
    bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey");
    when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
    when(mFuture.getResult()).thenReturn(bundle);
    when(mFuture.isDone()).thenReturn(true);
    when(mFuture.isCancelled()).thenReturn(false);
    Assert.assertEquals("monkey", mAuthenticator.getAuthToken());
}

From source file:drupalfit.sample.ToolBarActivity.java

/** {@inheritDoc} */
@Override/*from  w  ww. j  a va2s .  c  o m*/
public void addAccount(String username, String password, String authToken) {
    String accountType = "drualfit";
    String authTokenType = "drualfit";
    Account account = new Account(username, accountType);
    Bundle userdata = null;

    Intent intent = new Intent();

    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
    intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);

    if (TextUtils.isEmpty(password)) {
        boolean added = getAccountManager().addAccountExplicitly(account, password, userdata);
    } else {
        getAccountManager().setPassword(account, password);
    }

    getAccountManager().setAuthToken(account, authTokenType, authToken);

    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
}

From source file:com.android.volley.toolbox.AndroidAuthenticator.java

@SuppressWarnings("deprecation")
@Override//from  w ww. j  a  v  a2 s . c  om
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType,
            mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}

From source file:com.rukman.emde.smsgroups.authenticator.GMSAuthenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle options) throws NetworkErrorException {

    Log.d(TAG, "Get Auth Token");
    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(GMSApplication.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }/*from   www  . ja v  a 2 s . c o  m*/

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final String password = AccountManager.get(mContext).getPassword(account);
    if (!TextUtils.isEmpty(password)) {
        try {
            final String authToken = NetworkUtilities.authenticate(account.name, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity panel.
    final Intent intent = new Intent(mContext, GMSAuthenticatorActivity.class);
    intent.putExtra(GMSAuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(GMSAuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

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

@Override
public String getAuthToken(Context ctx, String inAuthority)
        throws OperationCanceledException, AuthenticatorException, IOException {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    AccountManager am = AccountManager.get(ctx);
    AccountManagerFuture<Bundle> future = am.getAuthToken(
            new Account(Constants.getAccountName(ctx), Constants.getAccountType(ctx)), authority, true, null,
            null);/*from w ww . j  a va  2 s .  com*/
    String token = null;
    if (future.isDone()) {
        token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    }
    return token;
}