Example usage for android.accounts AccountManager invalidateAuthToken

List of usage examples for android.accounts AccountManager invalidateAuthToken

Introduction

In this page you can find the example usage for android.accounts AccountManager invalidateAuthToken.

Prototype

public void invalidateAuthToken(final String accountType, final String authToken) 

Source Link

Document

Removes an auth token from the AccountManager's cache.

Usage

From source file:Main.java

public static void invalidateAuthToken(final Context context) {
    AccountManager am = AccountManager.get(context);
    am.invalidateAuthToken(ACCOUNT_TYPE, getAuthToken(context));
    setAuthToken(context, null);//from w w w .j  ava2s.  c  o  m
}

From source file:Main.java

/**
 * Invalidate auth token for specified account
 * @param account account to invalidate auth token
 * @param authTokenType auth token type//  w  w w.  j  a  v  a 2 s  .c  om
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void invalidateAuthToken(Account account, String authTokenType, String[] requiredFeatures,
        Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    if (account == null) {
        throw new IllegalArgumentException("account cannot be null");
    }
    Context context = activity.getApplicationContext();
    final AccountManager am = AccountManager.get(context);
    String authToken = am.peekAuthToken(account, authTokenType);
    if (!TextUtils.isEmpty(authToken)) {
        am.invalidateAuthToken(account.type, authToken);
    }
    am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
}

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

private static HttpResponse makeRequestNoRetry(Context context, String deviceRegistrationID, String url,
        boolean newToken) throws Exception {
    // Get chosen user account
    SharedPreferences settings = Prefs.get(context);
    String accountName = settings.getString("accountName", null);
    if (accountName == null)
        throw new Exception("No account");

    // Get auth token for account
    Account account = new Account(accountName, "com.google");
    String authToken = getAuthToken(context, account);
    if (authToken == null) {
        throw new PendingAuthException(accountName);
    }/*from  ww  w. j  a  va  2 s  .c o m*/
    if (newToken) {
        // Invalidate the cached token
        AccountManager accountManager = AccountManager.get(context);
        accountManager.invalidateAuthToken(account.type, authToken);
        authToken = getAuthToken(context, account);
    }

    // Register device with server
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = url + "?devregid=" + deviceRegistrationID;
    URI uri = new URI(AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken);
    HttpGet method = new HttpGet(uri);
    HttpResponse res = client.execute(method);
    return res;
}

From source file:com.ntsync.android.sync.client.NetworkUtilities.java

private static String retryAuthentification(int retryCount, AccountManager accountManager, String authtoken,
        String accountName, HttpResponse response)
        throws AuthenticationException, OperationCanceledException, NetworkErrorException, ServerException {
    accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken);
    String newToken = null;/*from www  .j  a  v a 2s.  co m*/
    if (retryCount == 0) {
        newToken = blockingGetAuthToken(accountManager, new Account(accountName, Constants.ACCOUNT_TYPE), null);
    }
    if (newToken == null) {
        throw new AuthenticationException(response.getStatusLine().toString());
    }
    return newToken;
}

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleAPITalker.java

private static String getAuthToken(AccountManager accountManager, Account account, String authTokenType,
        boolean notifyAuthFailure) {

    Log.d(TAG, "getAuthToken");
    String authToken = "";
    try {/*w  ww . jav  a  2 s.c o  m*/
        // Might be invalid in the cache
        authToken = accountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure);
        accountManager.invalidateAuthToken("com.google", authToken);

        authToken = accountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure);
    } catch (OperationCanceledException ignored) {
    } catch (AuthenticatorException ignored) {
    } catch (IOException ignored) {
    }
    return authToken;
}

From source file:com.browsertophone.AppEngineClient.java

private HttpResponse makeRequestNoRetry(String urlPath, List<NameValuePair> params, boolean newToken)
        throws Exception {

    // Get auth token for account
    Account account = new Account(mAccountName, "com.google");
    String authToken = getAuthToken(mContext, account);
    if (authToken == null)
        throw new PendingAuthException(mAccountName);
    if (newToken) { // invalidate the cached token
        AccountManager accountManager = AccountManager.get(mContext);
        accountManager.invalidateAuthToken(account.type, authToken);
        authToken = getAuthToken(mContext, account);
    }//from  w w  w . j av  a2  s  .c  o m

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = BASE_URL;
    URI uri = new URI(AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");
    if (res.getStatusLine().getStatusCode() != 302 || headers.length == 0) {
        return res;
    }

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }

    // Make POST request
    uri = new URI(BASE_URL + urlPath);
    HttpPost post = new HttpPost(uri);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);
    post.setHeader("Cookie", ascidCookie);
    post.setHeader("X-Same-Domain", "1"); // XSRF
    res = client.execute(post);
    return res;
}

From source file:com.menumomma.chrome2phone.AppEngineClient.java

private HttpResponse makeRequestNoRetry(String urlPath, List<NameValuePair> params, boolean newToken)
        throws Exception {
    // Get auth token for account
    Account account = new Account(mAccountName, "com.google");
    String authToken = getAuthToken(mContext, account);

    if (newToken) { // invalidate the cached token
        AccountManager accountManager = AccountManager.get(mContext);
        accountManager.invalidateAuthToken(account.type, authToken);
        authToken = getAuthToken(mContext, account);
    }//from   w  w  w .j a v  a2  s .  c om

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = Config.BASE_URL;
    URI uri = new URI(AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");
    if (res.getStatusLine().getStatusCode() != 302 || headers.length == 0) {
        return res;
    }

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }

    // Make POST request
    uri = new URI(Config.BASE_URL + urlPath);
    HttpPost post = new HttpPost(uri);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);
    post.setHeader("Cookie", ascidCookie);
    post.setHeader("X-Same-Domain", "1"); // XSRF
    res = client.execute(post);
    return res;
}

From source file:com.lvlstudios.android.gtmessage.AppEngineClient.java

private HttpResponse makeRequestNoRetry(String urlPath, List<NameValuePair> params, boolean newToken)
        throws Exception {
    Log.d(TAG, "AppEngineClient.makeRequestNoRetry: " + urlPath);
    // Get auth token for account - needs to be Google account so can piggy back on Google services.
    Account account = new Account(mAccountName, "com.google");
    String authToken = getAuthToken(mContext, account);

    if (newToken) { // invalidate the cached token
        AccountManager accountManager = AccountManager.get(mContext);
        accountManager.invalidateAuthToken(account.type, authToken);
        authToken = getAuthToken(mContext, account);
    }//from www  .  j a  va2 s .  c  o m

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = BASE_URL;
    URI uri = new URI(AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");
    if (res.getStatusLine().getStatusCode() != 302 || headers.length == 0) {
        Log.w(TAG, "AppEngineClient " + urlPath + " authentication failed: "
                + res.getStatusLine().getStatusCode());
        return res;
    }

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }

    // Make POST request
    uri = new URI(BASE_URL + urlPath);
    HttpPost post = new HttpPost(uri);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);
    post.setHeader("Cookie", ascidCookie);
    post.setHeader("X-Same-Domain", "1"); // XSRF
    res = client.execute(post);
    if (res.getStatusLine().getStatusCode() != 200) {
        Log.e(TAG, "AppEngineClient " + urlPath + " POST status: " + res.getStatusLine().getStatusCode());
    }
    return res;
}

From source file:com.prestomation.android.sospy.monitor.AppEngineClient.java

public String getASCIDCookie(boolean https) throws Exception {

    // Get auth token for account
    Account account = new Account(mAccountName, "com.google");
    String authToken = getAuthToken(mContext, account);
    if (authToken == null) {
        throw new PendingAuthException(mAccountName);
    }/*  ww w  . jav  a 2 s  .  c  om*/
    AccountManager accountManager = AccountManager.get(mContext);
    accountManager.invalidateAuthToken(account.type, authToken);
    authToken = getAuthToken(mContext, account);

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = BASE_URL;
    String sURI = AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken;
    if (https == false) {
        sURI = sURI.replace("https", "http");
    }
    URI uri = new URI(sURI);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not
    // used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");
    if (res.getStatusLine().getStatusCode() != 302 || headers.length == 0) {
        return res.toString();
    }

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }
    Log.i(TAG, "Received ASCIDCookie: " + ascidCookie);
    return ascidCookie;
}

From source file:com.notifry.android.remote.BackendClient.java

private HttpResponse requestNoRetry(String urlPath, List<NameValuePair> params, boolean newToken)
        throws Exception {
    // Get auth token for account
    Account account = new Account(this.accountName, "com.google");
    String authToken = getAuthToken(this.context, account);
    if (authToken == null) {
        throw new PendingAuthException(this.accountName);
    }/*from w ww  .  java2s  .  c  om*/
    if (newToken) {
        // Invalidate the cached token
        AccountManager accountManager = AccountManager.get(this.context);
        accountManager.invalidateAuthToken(account.type, authToken);
        authToken = this.getAuthToken(this.context, account);
    }

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = this.backendName;
    URI uri = new URI(this.backendName + "/_ah/login?continue=" + URLEncoder.encode(continueURL, "UTF-8")
            + "&auth=" + authToken);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not
    // used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }

    // Make POST request
    uri = new URI(this.backendName + urlPath);
    HttpPost post = new HttpPost(uri);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);
    post.setHeader("Cookie", ascidCookie);
    post.setHeader("X-Same-Domain", "1"); // XSRF
    res = client.execute(post);
    return res;
}