3

I have just integrated a Google+ Sign in button for my app. Everything works perfectly when you sign in, grant access and get the people information. However, I have a feeling I have one huge flaw in how I am doing this that may prevent a proper G+ sign out.

I have a LoginActivity where I create the mGoogleApiClient instance. As soon as I do the Login Check and update my DB and var's with the persons info, I finish() theLoginActivity and go to my MainActivity. This is where the user can choose to sign out.

However, I am afraid that I need the same instance of mGoogleApiClient; but this was wiped when I finished the LoginActivity. So I slightly modified things and tried to store it as a static var in another Activity I ahve where I use some variables globally -- that extends Application, but it doesn't seem to be signing out.

Am I on the right path or do I need to do this differently?

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • http://stackoverflow.com/questions/12909332/how-to-logout-of-an-application-where-i-used-oauth2-to-login-with-google – kosa Aug 25 '14 at 16:01
  • @Nambari isn't that for Javascript? – TheLettuceMaster Aug 25 '14 at 16:03
  • It is inherent limitation in oAuth and OpenID protocols, not specific to any programming language. https://groups.google.com/forum/#!topic/twitter4j/yTF2R0AmeIg – kosa Aug 25 '14 at 16:04
  • 1
    You could use an Application Controller to save the exact instance of GoogleApiClient and then load it for logout from Google account. http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – Jakub Turcovsky Aug 25 '14 at 16:15
  • Ok, I'm going with this approach. Creating a singleton to keep it open. Let's see if it works... – TheLettuceMaster Aug 25 '14 at 18:00
  • Let us know when it's done and how it works for you (or if you find different solution). – Jakub Turcovsky Aug 26 '14 at 08:25
  • @2rec I sort of took an easy way out so I could move on to something else. I needed the Disconnect Account button more than the Sign Out button (but the same principle of the question still applies). So I added that just underneath the Sign In button (on `LoginActivity`). therefore, I am keeping the same instance I used to signin to disconnect. Correct me if I'm wrong, but I don't need to sign out user every time. By closing the Activity with the instance, does the same I believe? I have another Sign out button that does more local things. – TheLettuceMaster Aug 27 '14 at 16:07

2 Answers2

1

In your new activity, do this:

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_LOGIN)
        .build();
mGoogleApiClient.connect();
// connect to current session

Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
// clear account
mGoogleApiClient.disconnect();
// disconnect
Hounshell
  • 5,321
  • 4
  • 34
  • 51
user1282637
  • 1,827
  • 5
  • 27
  • 56
  • Imho it's creating a new GoogleApiClient, I've tried to re-create it like this and exception occured. – Jakub Turcovsky Aug 25 '14 at 16:08
  • It doesn't get the same instance, but you don't need the same instance. You ***do*** need to wait until mGoogleApiClient is connected though. – Hounshell Sep 03 '14 at 20:46
  • any one can help me after login google plus how to sign out from another activity i need after login display another activity and then from that activity for logout please help me – Harsha Jul 25 '15 at 08:49
0

you need to connect to google play service again in your MainActivity with all the same permissions you have in your LoginActivity so that you can use that instance to sign-out

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • You do not need all of the same permissions. A connected GoogleApiClient with the Plus.API is enough to clear the default account or revoke access. – Hounshell Sep 03 '14 at 20:44
  • @Hounshell that is somewhat true, however if you add a new permission in a different activity such as google drive for example you need to resolve the signin problems before you get access to the API client so yes you need the same permissions persay – tyczj Sep 03 '14 at 20:48
  • 1
    "all of the same permissions" implies that a subset is not adequate. May I suggest verbiage along the lines of "without any permissions the user hasn't granted yet"? – Hounshell Sep 03 '14 at 20:50