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.browsertophone.AppEngineClient.java

private String getAuthToken(Context context, Account account) {
    String authToken = null;//ww w.ja v a  2  s. c o  m
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, false, null,
                null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            // No auth token - will need to ask permission from user.
            Intent intent = new Intent(SetupActivity.AUTH_PERMISSION_ACTION);
            intent.putExtra("AccountManagerBundle", bundle);
            context.sendBroadcast(intent);
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }
    return authToken;
}

From source file:sg.macbuntu.android.pushcontacts.DeviceRegistrar.java

private static String getAuthToken(Context context, Account account) {
    String authToken = null;// w w  w . j  a  v  a 2  s . co m
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, false, null,
                null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            // No auth token - will need to ask permission from user.
            Intent intent = new Intent(ActivityUI.AUTH_PERMISSION_ACTION);
            intent.putExtra("AccountManagerBundle", bundle);
            context.sendBroadcast(intent);
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }
    return authToken;
}

From source file:eu.trentorise.smartcampus.eb.HomeActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SCAccessProvider.SC_AUTH_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            String token = data.getExtras().getString(AccountManager.KEY_AUTHTOKEN);
            if (token == null) {
                EBHelper.endAppFailure(this, R.string.app_failure_security);
            } else {
                initData(token);//from w  w  w .j a  v a  2s.  c om
            }
        } else if (resultCode == RESULT_CANCELED) {
            EBHelper.endAppFailure(this, eu.trentorise.smartcampus.ac.R.string.token_required);
        }
    }
}

From source file:com.dhara.googlecalendartrial.MainActivity.java

private void getAccounts() {
    accountManager = AccountManager.get(this.getBaseContext());
    Account[] accounts = accountManager.getAccountsByType("com.google");
    account = accounts[0];//from ww  w  . ja  v a 2s .  co  m
    Log.e("tag", "acc : " + account.name + " ");
    accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, MainActivity.this,
            new AccountManagerCallback<Bundle>() {
                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 you can use the Tasks API...
                        useCalendarAPI(token, account.name);
                    } catch (OperationCanceledException e) {
                        // TODO: The user has denied you access to the API, you should handle that
                    } catch (Exception e) {
                        e.printStackTrace();

                        t.send(new HitBuilders.ExceptionBuilder().setDescription(Utilities.getMessage(e))
                                //.setDescription(new StandardExceptionParser(MainActivity.this, null).getDescription(Thread.currentThread().getName(), e))
                                .setFatal(false).build());
                    }
                }
            }, null);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(getString(R.string.path));

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());
}

From source file:com.todoroo.astrid.welcome.tutorial.WelcomeWalkthrough.java

private void initializeSimpleUI(final String email) {
    Button simpleLogin = (Button) findViewById(R.id.quick_login_google);
    simpleLogin.setText(getString(R.string.actfm_quick_login, email));
    simpleLogin.setOnClickListener(new OnClickListener() {
        @Override//  www  .  j  ava  2s .c  om
        public void onClick(View v) {
            StatisticsService.reportEvent(StatisticsConstants.ACTFM_LOGIN_SIMPLE);
            final ProgressDialog pd = DialogUtilities.progressDialog(WelcomeWalkthrough.this,
                    getString(R.string.gtasks_GLA_authenticating));
            pd.show();
            getAuthToken(email, pd);
        }

        private void getAuthToken(final String e, final ProgressDialog pd) {
            final GoogleAccountManager accountManager = new GoogleAccountManager(WelcomeWalkthrough.this);
            Account a = accountManager.getAccountByName(e);
            AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {
                public void run(final AccountManagerFuture<Bundle> future) {
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                Bundle bundle = future.getResult(30, TimeUnit.SECONDS);
                                if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                                    authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                                    if (!onSuccess) {
                                        accountManager.manager.invalidateAuthToken(
                                                ActFmGoogleAuthActivity.AUTH_TOKEN_TYPE, authToken);
                                        getAuthToken(e, pd);
                                        onSuccess = true;
                                    } else {
                                        onAuthTokenSuccess(e, authToken);
                                        dismissDialog = true;
                                    }
                                } else {
                                    dismissDialog = true;
                                }
                            } catch (final Exception e) {
                                Log.e("actfm-google-auth", "Login Error", e); //$NON-NLS-1$ //$NON-NLS-2$
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        int error = e instanceof IOException ? R.string.gtasks_GLA_errorIOAuth
                                                : R.string.gtasks_GLA_errorAuth;
                                        Toast.makeText(WelcomeWalkthrough.this, error, Toast.LENGTH_LONG)
                                                .show();
                                        onAuthError();
                                    }
                                });
                            } finally {
                                if (dismissDialog)
                                    DialogUtilities.dismissDialog(WelcomeWalkthrough.this, pd);
                            }
                        }
                    }.start();
                }
            };
            accountManager.manager.getAuthToken(a, ActFmGoogleAuthActivity.AUTH_TOKEN_TYPE, null,
                    WelcomeWalkthrough.this, callback, null);
        }
    });

    TextView rejectQuickLogin = (TextView) findViewById(R.id.quick_login_reject);
    rejectQuickLogin.setText(getString(R.string.actfm_quick_login_reject, email));
    rejectQuickLogin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            StatisticsService.reportEvent(StatisticsConstants.ACTFM_LOGIN_SIMPLE_REJECTED);
            switchToLoginPage();
        }
    });

    errors = (TextView) findViewById(R.id.error);
}

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  w  w  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:com.lvlstudios.android.gtmessage.AppEngineClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;/*from  w  ww  .j  a  va2  s. co  m*/
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, false, null,
                null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        if (authToken == null) {
            throw new PendingAuthException(bundle);
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    } catch (AuthenticatorException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    } catch (IOException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    }
    return authToken;
}

From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java

/**
 * {@inheritDoc}/*from w  ww .  j  a v  a 2  s . c  o m*/
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) {
    if (!authTokenType.equals(AuthenticationService.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }
    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);
    if (password != null) {
        final Bundle accountData = onlineConfirmPassword(account, password);
        if (accountData != null) {
            final Bundle result = new Bundle();

            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, AuthenticationService.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, password);
            return result;
        }
    }
    // the password was missing or incorrect, return an Intent to an
    // Activity that will prompt the user for the password.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.EXTRA_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:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java

/**
 * {@inheritDoc}//from   ww  w.j av  a  2  s .  c  o  m
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) {
    if (!authTokenType.equals(getAuthTokenType())) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }
    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);
    if (password != null) {
        final Bundle accountData = onlineConfirmPassword(account, password);
        if (accountData != null) {
            final Bundle result = new Bundle();

            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, getAccountType());
            result.putString(AccountManager.KEY_AUTHTOKEN, password);
            return result;
        }
    }
    // the password was missing or incorrect, return an Intent to an
    // Activity that will prompt the user for the password.
    final Intent intent = getAuthenticator(mContext);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_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:com.nextgis.maplibui.activity.NGWLoginActivity.java

@Override
public void onAddAccount(Account account, String token, boolean accountAdded) {
    if (null != account) {
        mResultBundle = new Bundle();

        if (accountAdded) {
            mResultBundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            mResultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
            mResultBundle.putString(AccountManager.KEY_AUTHTOKEN, token);
        } else {//from w w  w.j  av  a2s. c  o  m
            mResultBundle.putString(AccountManager.KEY_ERROR_MESSAGE,
                    getString(R.string.account_already_exists));
        }
    }

    setResult(RESULT_OK);
    finish();
}