GoogleAccountCredential

GoogleAccountCredential

本文介绍了如何使用Google帐户凭据OAuth2(Java)从Google注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I implement Google login using GoogleAccountCredential method.

通过 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 注销.

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

这是供youtube访问的.因此,我必须在OAuth2中使用 GoogleAccountCredential .

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

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

试图清除键 PREF_ACCOUNT_NAME SharedPreferences 没用

推荐答案

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

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

这篇关于如何使用Google帐户凭据OAuth2(Java)从Google注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 17:48