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:eu.trentorise.smartcampus.jp.notifications.BroadcastNotificationsActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SCAccessProvider.SC_AUTH_ACTIVITY_REQUEST_CODE) {

        try {//www. j a va  2  s  . c  om
            SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
            MobilityUserService userService = new MobilityUserService(
                    GlobalConfig.getAppUrl(this) + JPHelper.MOBILITY_URL);
            if (sharedPref.contains(JPHelper.MY_ITINERARIES)) {
                String mToken = data.getExtras().getString(AccountManager.KEY_AUTHTOKEN);
                if (mToken == null) {
                    Toast.makeText(this, getString(R.string.auth_failed), Toast.LENGTH_SHORT).show();
                } else {

                    JPHelper.setUserAnonymous(this, false);
                    invalidateOptionsMenu();
                    JPHelper.readAccountProfile(
                            new CopyTask(sharedPref, userService, resultCode, data, this, this));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

From source file:com.github.riotopsys.shoppinglist.activity.ShoppingListPreview.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_list_preview);

    AccountUtils accountUtils = new AccountUtils();
    String account = accountUtils.getAccountName(this);

    if (account == null) {
        AccountManager am = AccountManager.get(this);
        am.getAuthTokenByFeatures(AppKeys.ACCOUNT_TYPE, AppKeys.OAUTH2_SCOPE, null, this, null, null,
                new AccountManagerCallback<Bundle>() {
                    @Override/*from   w  w w .j  a v a 2s  . c  o m*/
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle bundle = future.getResult();
                            Log.i(TAG, "Got Bundle:\n" + " act name: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_NAME) + "\n act type: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) + "\n auth token: "
                                    + bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            AccountUtils accountUtils = new AccountUtils();
                            accountUtils.setAccountName(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
                            accountUtils.setToken(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_AUTHTOKEN));
                        } catch (Exception e) {
                            Log.i(TAG, "getAuthTokenByFeatures() cancelled or failed:", e);
                            Toast.makeText(getBaseContext(), R.string.no_account, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    }
                }, null);
    }

    mShoppingListCollection = new ShoppingListCollection();

    mShoppingListCollectionAdapter = new ShoppingListCollectionAdapter(this, getSupportFragmentManager(),
            mShoppingListCollection);

    //      mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mShoppingListCollectionAdapter);

    Intent startingIntent = getIntent();
    if (startingIntent != null) {
        Uri data = startingIntent.getData();
        if (data != null) {
            UUID guid = UUID.fromString(data.getLastPathSegment());
            Intent i = new Intent(this, ServerInterfaceService.class);
            i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.SUBSCRIBE);
            i.putExtra(AppKeys.GUID_KEY, guid);
            startService(i);
        }
    }

    IConfigurations config = new Configurations();

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        GCMRegistrar.register(this, config.getGCMSenderID());
    } else {
        Log.v(TAG, "Already registered");
    }

}

From source file:org.klnusbaum.udj.auth.Authenticator.java

private Bundle bundleUpAuthToken(Account account, String authToken) {
    final Bundle result = new Bundle();
    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
    return result;
}

From source file:com.clearcenter.mobile_demo.mdAuthenticator.java

public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(mdConstants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }//from  w w  w  .  ja va 2  s . c  o m

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final AccountManager am = AccountManager.get(ctx);
    final String password = am.getPassword(account);
    final String hostname = am.getUserData(account, "hostname");
    final String username = am.getUserData(account, "username");

    if (password != null) {
        try {
            mdSSLUtil.DisableSecurity();

            final String authToken = mdRest.Login(hostname, username, null, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, mdConstants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "GeneralSecurityException", e);
        }
    }

    Log.v(TAG, "Asking for password again...");

    // 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 mdAuthenticatorActivity panel.
    final Intent intent = new Intent(ctx, mdAuthenticatorActivity.class);
    intent.putExtra(mdAuthenticatorActivity.PARAM_NICKNAME, account.name);
    intent.putExtra(mdAuthenticatorActivity.PARAM_USERNAME, username);
    intent.putExtra(mdAuthenticatorActivity.PARAM_HOSTNAME, hostname);
    intent.putExtra(mdAuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(mdAuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    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.example.jumpnote.android.jsonrpc.AuthenticatedJsonRpcJavaClient.java

public void blockingAuthenticateAccount(final Account account, final int needAuthAction,
        boolean forceReauthenticate) throws AuthenticationException, OperationCanceledException,
        RequestedUserAuthenticationException, InvalidAuthTokenException {

    String existingToken = mTokenStoreHelper.getToken(account);
    if (!forceReauthenticate && existingToken != null) {
        BasicClientCookie c = new BasicClientCookie("ACSID", existingToken);
        try {/*from w w  w .j  a va2 s  . c  om*/
            c.setDomain(new URI(Config.SERVER_BASE_URL).getHost());
            mHttpClient.getCookieStore().addCookie(c);
            return;
        } catch (URISyntaxException e) {
        }
    }

    // Get an auth token for this account.
    AccountManager am = AccountManager.get(mContext);
    Bundle authBundle = null;
    String authToken = null;

    // Block on getting the auth token result.
    try {
        authBundle = am.getAuthToken(account, APPENGINE_SERVICE_NAME, needAuthAction == NEED_AUTH_NOTIFICATION,
                null, null).getResult();
    } catch (IOException e) {
        throw new AuthenticationException("IOException while getting auth token.", e);
    } catch (AuthenticatorException e) {
        throw new AuthenticationException("AuthenticatorException while getting auth token.", e);
    }

    if (authBundle.containsKey(AccountManager.KEY_INTENT) && needAuthAction == NEED_AUTH_INTENT) {
        Intent authRequestIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        mContext.startActivity(authRequestIntent);
        throw new RequestedUserAuthenticationException();
    } else if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
    }

    if (authToken == null) {
        throw new AuthenticationException("Retrieved auth token was null.");
    }

    try {
        blockingAuthenticateWithToken(account, authToken);
    } catch (InvalidAuthTokenException e) {
        am.invalidateAuthToken(account.type, authToken);
        throw e;
    }
}

From source file:com.samsung.android.remindme.jsonrpc.AuthenticatedJsonRpcJavaClient.java

public void blockingAuthenticateAccount(final Account account, final int needAuthAction,
        boolean forceReauthenticate) throws AuthenticationException, OperationCanceledException,
        RequestedUserAuthenticationException, InvalidAuthTokenException {

    String existingToken = mTokenStoreHelper.getToken(account);
    if (!forceReauthenticate && existingToken != null) {
        BasicClientCookie c = new BasicClientCookie("ACSID", existingToken);
        try {//  w w  w  .j  a va2  s .c o m
            c.setDomain(new URI(Config.SERVER_BASE_URL).getHost());
            mHttpClient.getCookieStore().addCookie(c);
            return;
        } catch (URISyntaxException e) {
        }
    }

    // Get an auth token for this account.
    AccountManager am = AccountManager.get(mContext);
    Bundle authBundle = null;
    String authToken = null;

    // Block on getting the auth token result.
    try {
        authBundle = am.getAuthToken(account, APPENGINE_SERVICE_NAME, needAuthAction == NEED_AUTH_NOTIFICATION,
                null, null).getResult();
    } catch (IOException e) {
        throw new AuthenticationException("IOException while getting auth token.", e);
    } catch (AuthenticatorException e) {
        throw new AuthenticationException("AuthenticatorException while getting auth token.", e);
    }

    if (authBundle.containsKey(AccountManager.KEY_INTENT) && needAuthAction == NEED_AUTH_INTENT) {
        Intent authRequestIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        mContext.startActivity(authRequestIntent);
        throw new RequestedUserAuthenticationException();
    } else if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
        System.out.println(authToken);
        System.out.println(AccountManager.KEY_AUTHTOKEN);
    }

    if (authToken == null) {
        throw new AuthenticationException("Retrieved auth token was null.");
    }

    try {
        blockingAuthenticateWithToken(account, authToken);
    } catch (InvalidAuthTokenException e) {
        am.invalidateAuthToken(account.type, authToken);
        throw e;
    }
}

From source file:pt.up.mobile.authenticator.Authenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }//from   w  ww . ja v a  2  s .  co m
    try {
        final AccountManager am = AccountManager.get(mContext);
        final String peek = am.peekAuthToken(account, authTokenType);
        if (peek != null) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, peek);
            return result;
        }
        // Extract the username and password from the Account Manager, and
        // ask
        // the server for an appropriate AuthToken.
        final String password = am.getPassword(account);
        if (password != null) {
            String[] reply;

            reply = SifeupAPI.authenticate(account.name, password, mContext);
            final String authToken = reply[1];
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        }

    } catch (AuthenticationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        throw new NetworkErrorException();
    }
    // 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, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.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:com.friedran.appengine.dashboard.client.AppEngineDashboardAuthenticator.java

protected void onGetAuthToken(Bundle bundle) {
    mAuthToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
    LogUtils.i("AppEngineDashboardAuthenticator", "onGetAuthToken: Got the auth token " + mAuthToken);

    if (mAuthToken == null) {
        // Failure, looks like an illegal account
        mPostAuthenticateCallback.run(false);
    } else {//from  w ww  .  j a  va  2s . c om
        new LoginToAppEngineTask().execute();
    }
}

From source file:com.noswap.keyring.MainActivity.java

public void doGCMStuff() {
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        Log.v(TAG, "Registering");
        GCMRegistrar.register(this, SENDER_ID);
    } else {/*from  w  w  w. j av a 2 s  . c o  m*/
        Log.v(TAG, "Already registered: " + regId);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType("com.google");
    for (Account account : accounts) {
        Log.v(TAG, "Account: " + account.name + " (" + account.type + ")");
    }

    if (accounts.length > 0) {
        Account account = accounts[0];
        Bundle options = new Bundle();
        am.getAuthToken(account, "Keyring", options, this, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> result) {
                try {
                    Bundle bundle = result.getResult();
                    String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                    if (intent != null) {
                        startActivityForResult(intent, 0);
                        return;
                    }
                    Log.v(TAG, "onTokenAcquired: " + token);
                } catch (Exception e) {
                    Log.v(TAG, "onTokenAcquired exception: " + e.toString());
                }
            }
        }, new Handler() {
        });

    }
}

From source file:eu.trentorise.smartcampus.launcher.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        String token = data.getExtras().getString(AccountManager.KEY_AUTHTOKEN);
        if (token == null) {
            Toast.makeText(this, getString(R.string.auth_failed), Toast.LENGTH_SHORT).show();
        }/*from   w  ww  .  j  av a2  s  .c  o  m*/
    } else {
        Toast.makeText(this, getString(R.string.token_required), Toast.LENGTH_LONG).show();
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}