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.notifry.android.remote.BackendClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//  ww  w .  ja  v a2s.c o  m
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, "ah", 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 = (Intent) bundle.get(AccountManager.KEY_INTENT);
            if (intent != null) {
                // User input required
                context.startActivity(intent);
                throw new PendingAuthException("Asking user for permission.");
            }
        }
    } 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:com.owncloud.android.network.OwnCloudClientUtils.java

public static WebdavClient createOwnCloudClient(Account account, Context appContext, Activity currentActivity)
        throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
    Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
    AccountManager am = AccountManager.get(appContext);
    boolean isOauth2 = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
    boolean isSamlSso = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null;
    WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);

    if (isOauth2) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token

    } else if (isSamlSso) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE, null, currentActivity, null,
                null);//from www  .  java  2s  .  c o m
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setSsoSessionCookie(accessToken);

    } else {
        String username = account.name.substring(0, account.name.lastIndexOf('@'));
        //String password = am.getPassword(account);
        //String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false);
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String password = result.getString(AccountManager.KEY_AUTHTOKEN);
        client.setBasicCredentials(username, password);
    }

    return client;
}

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (mHelper == null) {
        mHelper = new CaptureHelper(this, 0, this);
    }// w  w w.  ja va 2 s .c  o m
    mHelper.onCaptureResult(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);
            }
        } else if (resultCode == RESULT_CANCELED) {
            EBHelper.endAppFailure(this, eu.trentorise.smartcampus.ac.R.string.token_required);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

From source file:com.agiro.scanner.android.AppEngineClient.java

private String getAuthToken(Context context, Account account) {
    String authToken = null;/*ww w.ja v  a 2s  .  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();
        Account[] accs = accountManager.getAccounts();
        Log.v(TAG, "Account size = " + accs.length);
        Log.v(TAG, "Listing accounts");
        for (Account acc : accs) {
            Log.v(TAG, "Account: " + acc);
        }
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            Log.e(TAG, "No authToken");
            // No auth token - will need to ask permission from user.
            Intent intent = new Intent("com.google.ctp.AUTH_PERMISSION");
            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:dev.drsoran.moloko.auth.AuthenticatorActivity.java

/**
 * Response is received from the server for authentication request. Sets the AccountAuthenticatorResult which is sent
 * back to the caller. Also sets the authToken in AccountManager for this account.
 *//*from  w w  w . j  a va 2 s . co m*/
@Override
public void onAuthenticationFinished(RtmAuth rtmAuth) {
    final Account account = new Account(rtmAuth.getUser().getUsername(), Constants.ACCOUNT_TYPE);
    try {
        boolean ok = true;

        if (isNewAccount) {
            ok = accountManager.addAccountExplicitly(account, rtmAuth.getToken(), null);
            if (ok) {
                ContentResolver.setSyncAutomatically(account, Rtm.AUTHORITY, true);
            }
        }

        if (ok) {
            accountManager.setUserData(account, Constants.FEAT_API_KEY, MolokoApp.getRtmApiKey(this));
            accountManager.setUserData(account, Constants.FEAT_SHARED_SECRET,
                    MolokoApp.getRtmSharedSecret(this));
            accountManager.setUserData(account, Constants.FEAT_PERMISSION, rtmAuth.getPerms().toString());
            accountManager.setUserData(account, Constants.ACCOUNT_USER_ID, rtmAuth.getUser().getId());
            accountManager.setUserData(account, Constants.ACCOUNT_FULLNAME, rtmAuth.getUser().getFullname());

            final Intent intent = new Intent();

            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, rtmAuth.getUser().getUsername());
            // We store the authToken as password
            intent.putExtra(AccountManager.KEY_PASSWORD, rtmAuth.getToken());
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, rtmAuth.getToken());

            intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);

            setAccountAuthenticatorResult(intent.getExtras());
            setResult(RESULT_OK, intent);
        }
    } catch (SecurityException e) {
        MolokoApp.Log.e(getClass(), e.getLocalizedMessage());
        onAuthenticationFailed(getString(R.string.auth_err_cause_scurity));
    } finally {
        finish();
    }
}

From source file:com.google.android.apps.chrometophone.AppEngineClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;/*from ww w.j  av  a2 s. c  om*/
    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, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }
    return authToken;
}

From source file:com.google.appinventor.components.runtime.util.ClientLoginHelper.java

/**
 * Uses Google Account Manager to retrieve auth token that can
 * be used to access various Google APIs -- e.g., the Google Voice api.
 *//*from w  w  w . ja va 2s  .c o  m*/
public String getAuthToken() throws ClientProtocolException {
    Account account = accountChooser.findAccount();
    if (account != null) {
        AccountManagerFuture<Bundle> future;
        future = accountManager.getAuthToken(account, service, null, activity, null, null);
        Log.i(LOG_TAG, "Have account, auth token: " + future);
        Bundle result;
        try {
            result = future.getResult();
            return result.getString(AccountManager.KEY_AUTHTOKEN);
        } catch (AuthenticatorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OperationCanceledException e) {
            e.printStackTrace();
        }
    }
    throw new ClientProtocolException("Can't get valid authentication token");
}

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

@Override
public String getAuthToken(Context ctx, String inAuthority, IntentSender intentSender)
        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, false,
            new OnTokenAcquired(ctx, authority, intentSender), null);
    String token = null;/*from  w  w  w .  java 2  s  . co m*/
    if (future.isDone()) {
        token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    }
    return token;
}

From source file:org.amahi.anywhere.fragment.NavigationFragment.java

@Override
public void run(AccountManagerFuture<Bundle> accountManagerFuture) {
    try {/*from www  .ja  va  2  s.  c om*/
        Bundle accountManagerResult = accountManagerFuture.getResult();

        String authenticationToken = accountManagerResult.getString(AccountManager.KEY_AUTHTOKEN);

        if (authenticationToken != null) {
            setUpServers(authenticationToken);
        } else {
            setUpAuthenticationToken();
        }
    } catch (OperationCanceledException e) {
        tearDownActivity();
    } catch (IOException | AuthenticatorException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.msal.shoutemo.connector.GetPostsService.java

/**
 *
 *//*from  w ww  . j  a  v a 2  s .  c o m*/
private void startGetPostsTask() {
    mAccountManager.getAuthToken(mAccount, LoginActivity.PARAM_AUTHTOKEN_TYPE, null, false,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle;
                    try {
                        bundle = result.getResult();
                    } catch (OperationCanceledException e) {
                        e.printStackTrace();
                        return;
                    } catch (AuthenticatorException e) {
                        e.printStackTrace();
                        return;
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    mAuthToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Log.v(TAG, "Received authentication token=" + mAuthToken);
                    // now get messages!
                    if (worker == null || worker.isShutdown()) {
                        worker = Executors.newSingleThreadScheduledExecutor();
                    }
                    // fix (possible) wrong time-setting on autemo.com
                    worker.execute(new SetTimezoneTask());
                    // only now recieve messages (with right time)
                    worker.scheduleAtFixedRate(new GetPostsTask(), 0, INTERVAL, TimeUnit.MILLISECONDS);
                }
            }, null);
}