Java Code Examples for android.accounts.AccountManager#getAuthToken()

The following examples show how to use android.accounts.AccountManager#getAuthToken() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 2
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 3
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 4
Source Project: volley   File: AndroidAuthenticator.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 5
Source Project: android-atleap   File: AuthHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if exist account, if not create one. Then check the authToken is up-to-date, if not try to authenticate.
 * The best place for this method in the begin of the onStart method of every activity.
 * @param accountType accountType
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options addAccountOptions, could be <code>null</code>
 * @param activity cannot be null
 * @return <code>true</code> if user is already authenticated
 */
public static boolean checkLastAccountAndToken(String accountType, String authTokenType, String[] requiredFeatures, Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    boolean isAuthenticated = false;
    Account account = getLastOrFirstAccount(activity.getApplicationContext(), accountType);
    final AccountManager am = AccountManager.get(activity.getApplicationContext());
    if (account == null) {
        am.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
    } else {
        isAuthenticated = !TextUtils.isEmpty(am.peekAuthToken(account, authTokenType));
        am.getAuthToken(account, authTokenType, options, activity, null, null);
    }
    return isAuthenticated;
}
 
Example 6
Source Project: android-atleap   File: AuthHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Recreate authToken for the specified account.
 * @param context context
 * @param account account
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity, could be <code>null</code>
 */
public static void reCreateAuthToken(Context context, Account account, String authTokenType, String[] requiredFeatures, Bundle options, Activity activity) {
    final AccountManager am = AccountManager.get(context);
    boolean isAccountExist = isAccountExist(context, account);
    if (!isAccountExist) {
        am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
        return;
    }

    String authToken = am.peekAuthToken(account, authTokenType);

    if (TextUtils.isEmpty(authToken)) {
        am.getAuthToken(account, authTokenType, options, activity, null, null);
        return;
    }
    am.invalidateAuthToken(account.type, authToken);
    am.getAuthToken(account, authTokenType, options, activity, null, null);
}
 
Example 7
Source Project: okulus   File: AndroidAuthenticator.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 8
Source Project: FeedListViewDemo   File: AndroidAuthenticator.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 9
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 10
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 11
Source Project: WayHoo   File: AndroidAuthenticator.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.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;
}
 
Example 12
public static AccountManagerFuture<Bundle> getAuthToken(AccountManager manager,
                                                        Account account, String authTokenType,
                                                        Bundle options,
                                                        boolean notifyAuthFailure,
                                                        AccountManagerCallback<Bundle> callback,
                                                        Handler handler) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        return manager.getAuthToken(account, authTokenType, options, notifyAuthFailure,
                callback, handler);
    } else {
        //noinspection deprecation
        return manager.getAuthToken(account, authTokenType, notifyAuthFailure, callback,
                handler);
    }
}
 
Example 13
/**
 * Get the stored data from a secure store, decrypting the data if needed.
 * @return The data store on the secure storage.
 */
public String retrieveStringData(AccountManager accountManager, Account account, String tokenType, AccountManagerCallback<Bundle> callback)
        throws UserNotAuthenticatedWrapperException, AuthenticatorException, OperationCanceledException, IOException {
    String data = null;

    // Try retrieving an access token from the account manager. The boolean #SHOW_NOTIF_ON_AUTHFAILURE in the invocation
    // tells Android to show a notification if the token can't be retrieved. When the
    // notification is selected, it will launch the intent for re-authorisation. You could
    // launch it automatically here if you wanted to by grabbing the intent from the bundle.
    AccountManagerFuture<Bundle> futureManager;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        //noinspection deprecation
        futureManager = accountManager.getAuthToken(
                account,
                tokenType,
                SHOW_NOTIF_ON_AUTHFAILURE,
                callback,
                null);
    }
    else {
        futureManager = accountManager.getAuthToken(
                account,
                tokenType,
                null,
                SHOW_NOTIF_ON_AUTHFAILURE,
                callback,
                null);
    }
    String encryptedToken = futureManager.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    if (encryptedToken != null) {
        data = dataEncUtils.decrypt(encryptedToken);
    }

    return data;
}
 
Example 14
private void onAccountSelected(Intent data) {
    String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);

    boolean found = false;
    AccountManager am = AccountManager.get(getContext());
    Account[] accounts = am.getAccountsByType(type);
    for (final Account account : accounts)
        if (name.equalsIgnoreCase(account.name)) {
            found = true;
            Log.i("Requesting token name=" + account.name);
            am.getAuthToken(
                    account,
                    EmailService.getAuthTokenType(type),
                    new Bundle(),
                    getActivity(),
                    new AccountManagerCallback<Bundle>() {
                        @Override
                        public void run(AccountManagerFuture<Bundle> future) {
                            try {
                                Bundle bundle = future.getResult();
                                String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                                if (token == null)
                                    throw new IllegalArgumentException("no token");
                                Log.i("Got token name=" + account.name);

                                onAuthorized(name, token);
                            } catch (Throwable ex) {
                                Log.e(ex);
                                tvError.setText(Log.formatThrowable(ex));
                                grpError.setVisibility(View.VISIBLE);

                                new Handler().post(new Runnable() {
                                    @Override
                                    public void run() {
                                        scroll.smoothScrollTo(0, tvError.getBottom());
                                    }
                                });
                            }
                        }
                    },
                    null);
            break;
        }

    if (!found) {
        boolean permission = Helper.hasPermission(getContext(), Manifest.permission.GET_ACCOUNTS);

        Map<String, String> crumb = new HashMap<>();
        crumb.put("type", type);
        crumb.put("count", Integer.toString(accounts.length));
        crumb.put("permission", Boolean.toString(permission));
        Log.breadcrumb("Gmail", crumb);

        Log.e("Account missing");

        tvError.setText(getString(R.string.title_no_account));
        grpError.setVisibility(View.VISIBLE);
    }
}
 
Example 15
private void getAccessTokenFromAccountManager(Account account, String authTokenType, Bundle options, AccountManagerCallback accountManagerCallback, Handler handler) {
    AccountManager accountManager = AccountManager.get(context);
    accountManager.getAuthToken(account, authTokenType, options, activity, accountManagerCallback, handler);
}