且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 16:48:16

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

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

成功登录日志:

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

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

So, for being more specific you can check for 200