如何使用Google帐户凭据OAuth2(Java)从Google注销

时间:2018-07-09 13:59:39

标签: java android oauth-2.0 google-login google-account

我使用GoogleAccountCredential方法实现Google登录。

通过onCreate方法初始化

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

点击按钮时

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();
    }
}

我已经成功登录。 如何使用GoogleAccountCredential注销。

这是供youtube访问的。因此,我必须将GoogleAccountCredential与OAuth2配合使用。

更新1 2018年7月12日

@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;
    }
}

试图清除SharedPreferences的键PREF_ACCOUNT_NAME无用

1 个答案:

答案 0 :(得分:0)

您必须像这样调用另一个线程:

 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;
}

成功日志输出:

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

因此,要更具体一点,您可以检查200