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

The following examples show how to use android.accounts.AccountManager#setAuthToken() . 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
static void createAccount(Context context, boolean microsub) {
    String domain = "http://example.com";
    String accessToken = "awesome";

    AccountManager am = AccountManager.get(context);
    Account account = new Account(domain, ACCOUNT_TYPE);
    am.addAccountExplicitly(account, null, null);
    am.setAuthToken(account, TOKEN_TYPE, accessToken);
    am.setUserData(account, "micropub_endpoint", domain + "/micropub");
    am.setUserData(account, "authorization_endpoint", domain + "/auth");
    am.setUserData(account, "micropub_media_endpoint", domain + "/media");
    am.setUserData(account, "token_endpoint", domain + "/token");
    am.setUserData(account, "author_name", "Indigenous");

    // Set first account.
    SharedPreferences.Editor editor = context.getSharedPreferences("indigenous", MODE_PRIVATE).edit();
    editor.putString("account", domain);
    editor.apply();
    editor.commit();
}
 
Example 2
Source Project: Capstone-Project   File: PredatorAccount.java    License: MIT License 5 votes vote down vote up
/**
 * Add a new account to the device for this application, this account can either be either
 * contain a client token or user token.
 *
 * @param context       Current context of the application
 * @param accountName   Name of the account
 * @param accountType   Account type
 * @param authToken     Authentication token
 * @param authTokenType Type of the authentication token
 */
public static void addAccount(Context context,
                              String accountName,
                              String accountType,
                              String authToken,
                              String authTokenType) {
    Account account = new Account(accountName, accountType);
    AccountManager accountManager = AccountManager.get(context);

    // Add a new account.
    accountManager.addAccountExplicitly(account, null, null);

    // Set the authentication code.
    accountManager.setAuthToken(account, authTokenType, authToken);
}
 
Example 3
private Bundle doAfterSignUpIsSuccessful(AccountManager accountManager, Account account, String authTokenType, SignUpResult signUpResult, Bundle options) {
    Bundle result;
    if (signUpResult.isSuccessful) {
        accountManager.setPassword(account, signUpResult.refreshToken);
        accountManager.setAuthToken(account, authTokenType, signUpResult.accessToken);
        result = makeResultBundle(account, signUpResult.refreshToken, signUpResult.accessToken);
        Toast.makeText(context, "New refresh token is acquired from the server", Toast.LENGTH_SHORT).show();
        signInAndDoAfter(account, authTokenType, signUpResult.accessToken, options);
        return result;
    } else {
        throw new RuntimeException("Sign-up is not successful due to the following:/n" + signUpResult.errMessage);
    }
}
 
Example 4
Source Project: tindroid   File: UiUtils.java    License: Apache License 2.0 5 votes vote down vote up
static void updateAndroidAccount(final Context context, final String uid, final String secret,
                                 final String token, final Date tokenExpires) {
    final AccountManager am = AccountManager.get(context);
    final Account acc = Utils.createAccount(uid);
    // It's OK to call even if the account already exists.
    am.addAccountExplicitly(acc, secret, null);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.notifyAccountAuthenticated(acc);
    }
    if (!TextUtils.isEmpty(token)) {
        am.setAuthToken(acc, Utils.TOKEN_TYPE, token);
        am.setUserData(acc, Utils.TOKEN_EXPIRATION_TIME, String.valueOf(tokenExpires.getTime()));
    }
}
 
Example 5
Source Project: Mover   File: AuthenticationActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Subscribe
public void onEvent(AuthenticationEvent event){
    Bundle result = event.getResult();
    setLockForm(false);

    if(event.isSuccess()){
        String password = mPasswordField.getText().toString();
        AccountManager manager = AccountManager.get(this);

        Bundle userData = new Bundle();
        userData.putString(USER_PICTURE_URL, result.getString(USER_PICTURE_URL));

        manager.addAccountExplicitly(mAccount, password, userData);
        manager.setAuthToken(mAccount, mSourceId, result.getString(KEY_AUTHTOKEN));

        mAuthenticatorResponse.onResult(result);
        mAuthenticatorResponse = null;

        setResult(RESULT_OK);
        finish();

    }else{
        int errorCode = result.getInt(KEY_ERROR_CODE);

        if(errorCode == ERROR_CODE_INVALID_USER_DATA){
            mUsernameField.setError(getString(R.string.sign_in_failed_with_wrong_data_username));
            mPasswordField.setError(getString(R.string.sign_in_failed_with_wrong_data_password));
        }
    }
}
 
Example 6
@Override
protected Void doInBackground(final Void... params) {

    if (contextReference == null) {
        return null;
    }

    final Context context = contextReference.get();
    if (context == null) {
        return null;
    }

    if (isCancelled()) {
        return null;
    }

    final String accountName = loginResult.getName();

    final AccountManager accountManager = AccountManager.get(context);

    boolean addingAccount = false;
    if (existingAccountIsAnonymous) {
        //Remove the existing account so we can add the new one.
        //TODO: Find a way to just change the name,
        //though we don't lose any ItemsContentProvider data when we delete an Account.
        LoginUtils.removeAnonymousAccount(context);
        addingAccount = true;
    } else if(!TextUtils.equals(existingAccountName, accountName)) {
        //Remove any existing account so we can add the new one.
        //TODO: Find a way to just change the name,
        if (!TextUtils.isEmpty(existingAccountName)) {
            LoginUtils.removeAccount(context, existingAccountName);
        }

        addingAccount = true;
    }


    final Account account = new Account(accountName, LoginUtils.ACCOUNT_TYPE);
    if (addingAccount) {
        //Note that this requires the AUTHENTICATE_ACCOUNTS permission on
        //SDK <=22:
        accountManager.addAccountExplicitly(account, null, null);
        LoginUtils.copyPrefsToAccount(context, accountManager, account);

        //Tell the SyncAdapter to sync whenever the network is reconnected:
        LoginUtils.setAutomaticAccountSync(context, account);
    }

    //TODO? ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true)

    //This is apparently not necessary, when updating an existing account,
    //if this activity was launched from our Authenticator, for instance if our
    //Authenticator found that the accounts' existing auth token was invalid.
    //Presumably it is necessary if this activity is launched from our app.
    //Note that this requires the AUTHENTICATE_ACCOUNTS permission on
    //SDK <=22.
    accountManager.setAuthToken(account, LoginUtils.ACCOUNT_AUTHTOKEN_TYPE, loginResult.getApiKey());

    return null;
}
 
Example 7
/**
 * Store the given serialized data onto a secure store, encrypting the data if needed.
 * @param data The data to store securely
 * @return true if the data was store, false otherwise.
 */
public boolean storeStringData(AccountManager accountManager, Account account, String tokenType, String data) throws UserNotAuthenticatedWrapperException {
    accountManager.setAuthToken(account, tokenType, dataEncUtils.encrypt(data));
    return true;
}