0

I implement Google login using GoogleAccountCredential method.

Init at onCreate method

 mCredential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff());

When button tap action

public void getResultsFromApi() throws IOException {
    if (!isGooglePlayServicesAvailable()) {
        acquireGooglePlayServices();
    } else if (mCredential.getSelectedAccountName() == null) {
        chooseAccount();
    } else if (!isDeviceOnline()) {
        Utils.showAlert(this, getResources().getString(R.string.no_internet));
    } else {
        new MakeRequestTask(mCredential).execute();
    }
}

And I got successfully logged in. How can I logout using GoogleAccountCredential.

This is for youtube access. So, I have to use GoogleAccountCredential with OAuth2.

UPDATE ONE 12 JULY 2018

@Override
protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode != RESULT_OK) {
                Utils.showAlert(this, getResources().getString(R.string.youtube_alert_for_install_playstore));
            } else {
                try {
                    //Some code
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
        case REQUEST_ACCOUNT_PICKER:
            if (resultCode == RESULT_OK && data != null &&
                    data.getExtras() != null) {
                String accountName =
                        data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null) {
                    SharedPreferences settings =
                            getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREF_ACCOUNT_NAME, accountName);
                    editor.apply();
                    mCredential.setSelectedAccountName(accountName);
                    try {
                    //Some code
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        case REQUEST_AUTHORIZATION:
            if (resultCode == RESULT_OK) {
                try {
                    //Some code
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

Tried to clear SharedPreferences for key PREF_ACCOUNT_NAME no use

Mathi Arasan
  • 869
  • 2
  • 10
  • 32

1 Answers1

0

You have to call another thread like this :

 public  class  signout extends  Thread {

 
    org.apache.http.HttpResponse response = null;
    public  signout (){
        Log.i(TAG, "signout Session : Called");
    }

    @Override
    public void run() {
        super.run();
        response =  response();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(response!=null){
                    Toast.makeText(mContext, "Sign Out Success", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "run: signOut: success "+response.toString() +" Response String : "+response.getStatusLine().toString());
                     // then remove Selected Account from preference : preferences particular Key is "PREF_ACCOUNT_NAME"
                        editor.putString(PREF_ACCOUNT_NAME, null);
                        editor.apply();
                }else {
                    Log.i(TAG, "run: signOut: fail");
                    Toast.makeText(mContext, "Sign Out Fail", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

}

public  org.apache.http.HttpResponse response()
{
    org.apache.http.HttpResponse response = null;
    try{
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/revoke?token="+mCredential.getToken());
        response = client.execute(post);
    }
    catch(IOException | GoogleAuthException e)
    {
        Log.i(TAG, "run: RevokeAcess: Fail: "+e.getMessage());
    }
    CookieManager.getInstance().removeAllCookie(); 

    return  response;
}

Log Output On Success :

run: signOut: success org.apache.http.message.BasicHttpResponse@.... Response String : HTTP/1.1 200 OK

So, for being more specific you can check for 200

Noor Hossain
  • 1,620
  • 1
  • 18
  • 25