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.meiste.greg.ptw.GAE.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private boolean reconnect() {
    final AccountManager mgr = AccountManager.get(mContext);
    final Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
    for (final Account acct : accts) {
        if (acct.name.equals(mAccountName)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                mgr.getAuthToken(acct, "ah", null, false, new AuthTokenCallback(), null);
            } else {
                mgr.getAuthToken(acct, "ah", false, new AuthTokenCallback(), null);
            }//from  w  w  w .  ja v  a 2s. c om
            return true;
        }
    }
    Util.log("Account " + mAccountName + " not found!");
    return false;
}

From source file:io.v.syncslides.SignInActivity.java

private void fetchUserProfile() {
    AccountManager manager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = manager.getAccountsByType("com.google");
    Account account = null;//  w  w  w  . j a  v a2s  .  co  m
    for (int i = 0; i < accounts.length; i++) {
        if (accounts[i].name.equals(mAccountName)) {
            account = accounts[i];
            break;
        }
    }
    if (account == null) {
        Log.e(TAG, "Couldn't find Google account with name: " + mAccountName);
        pickAccount();
        return;
    }
    manager.getAuthToken(account, OAUTH_SCOPE, new Bundle(), false, new OnTokenAcquired(), new Handler(msg -> {
        Log.e(TAG, "Error getting auth token: " + msg.toString());
        fetchUserProfileDone(null);
        return true;
    }));
}

From source file:io.v.android.apps.syncslides.SignInActivity.java

private void fetchUserProfile() {
    if (!FETCH_PROFILE) {
        fetchUserProfileDone(null);//from  ww w  .ja  v  a  2 s  .  co  m
        return;
    }
    AccountManager manager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = manager.getAccountsByType("com.google");
    Account account = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accounts[i].name.equals(mAccountName)) {
            account = accounts[i];
            break;
        }
    }
    if (account == null) {
        Log.e(TAG, "Couldn't find Google account with name: " + mAccountName);
        pickAccount();
        return;
    }
    manager.getAuthToken(account, OAUTH_SCOPE, new Bundle(), false, new OnTokenAcquired(),
            new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    Log.e(TAG, "Error getting auth token: " + msg.toString());
                    fetchUserProfileDone(null);
                    return true;
                }
            }));
}

From source file:com.sefford.beauthentic.activities.LoginActivity.java

void performLogin() {
    final AccountManager am = AccountManager.get(this);
    final Bundle data = new Bundle();
    data.putString(AuthenticAuthenticator.EXTRA_PASSWORD, etPassword.getText().toString());
    data.putInt(AuthenticAuthenticator.EXTRA_TYPE, AuthenticAuthenticator.Type.PASSWORD.ordinal());
    final Account account = new Account(etUsername.getText().toString(), AuthenticAuthenticator.ACCOUNT_TYPE);
    am.getAuthToken(account, "", data, true, new AccountManagerCallback<Bundle>() {
        @Override//from w ww.j  ava2s .  c o m
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                final Bundle result = future.getResult();
                if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                    Sessions.addAccount(am, account, etPassword.getText().toString(), Bundle.EMPTY);
                    am.setAuthToken(account, AuthenticAuthenticator.AUTHTOKEN_TYPE,
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    am.setUserData(account, AuthenticAuthenticator.EXTRA_TYPE,
                            Integer.toString(AuthenticAuthenticator.Type.PASSWORD.ordinal()));
                    notifyLoginToGCM(AuthenticAuthenticator.Type.PASSWORD.ordinal(), account.name,
                            etPassword.getText().toString(), result.getString(AccountManager.KEY_AUTHTOKEN));
                    googleApi
                            .saveCredential(
                                    new Credential.Builder(account.name)
                                            .setPassword(etPassword.getText().toString()).build(),
                                    new SmartlockCredentialCallback());
                } else {
                    Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
                }
            } catch (OperationCanceledException e) {
                Snackbar.make(vLoginForm, R.string.error_operation_cancelled, Snackbar.LENGTH_LONG).show();
            } catch (IOException e) {
                Snackbar.make(vLoginForm, R.string.error_not_connected_to_internet, Snackbar.LENGTH_LONG)
                        .show();
            } catch (AuthenticatorException e) {
                Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
            }
        }

    }, null);
}

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

@Override
public String getAuthToken(final Activity activity, String inAuthority)
        throws OperationCanceledException, AuthenticatorException, IOException {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    String token = am.peekAuthToken(
            new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)), authority);
    if (token == null) {
        final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
        Account[] accounts = am.getAccountsByType(Constants.getAccountType(activity));
        if (accounts == null || accounts.length == 0) {
            am.addAccount(Constants.getAccountType(activity), authority, null, null, null,
                    new Callback(authority, activity), null);
        } else {/*from www  .j  a  v  a  2 s. com*/
            am.getAuthToken(a, authority, null, null, new Callback(authority, activity), null);
        }
        return null;
    }
    return token;

}

From source file:com.listomate.activities.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from w  ww  .  ja  v  a  2s .c om*/
 * @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.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.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 account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.cloudtask1.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from  www  .j a  v a 2s  .  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)) {
            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();
                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();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.masteriti.manager.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from   w w  w.ja  v a 2  s.c  om
 * @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();
                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>() {
                    @Override
                    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>() {
                            @Override
                            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;
        }
    }
}

From source file:com.capstonecontrol.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from   ww w .java  2 s  . c  om*/
 * @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();
                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();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.myandroidremote.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from  ww  w  . j a  v  a2s. 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.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.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 account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                boolean result = prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            if (authCookie == null) {
                                mgr.invalidateAuthToken("com.google", authToken);
                            }
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}