android / fit-samples

Multiple samples showing the best practices using the Fit APIs on Android.
Apache License 2.0
152 stars 113 forks source link

GoogleSignIn.hasPermissions always return true even if I call disableFit #28

Open codingjeremy opened 5 years ago

codingjeremy commented 5 years ago

Issue by norio-agoop Wednesday Mar 27, 2019 at 09:06 GMT Originally opened as https://github.com/googlesamples/android-fit/issues/48


Hi,

The situation

I'm trying to enable/disable google fit in my app with a Switch View.

When I turn off this switch, I call below code.

Fitness.getConfigClient(context, account).disableFit()
                .addOnSuccessListener {
                    Timber.d("disconnectGoogleFit: onSuccess")
                }
                ...

After onSuccess, I turn on switch to connect google fit and call below.

if (GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions){
 return
}
GoogleSignIn.requestPermissions(
        this, // your activity
        GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
        GoogleSignIn.getLastSignedInAccount(this),
        fitnessOptions);

Nothing happens because GoogleSignIn.hasPermissions always return true.

What I want to do

I'd like to check whether Google fit permission is granted or not before calling GoogleSignIn.requestPermissions

Please tell me how to do this.

Thank you


Update

I think I have found a solution that disconnecting app through Google Fit App. After this, GoogleSignIn.hasPermissions return false. But I am curious how can do this programmatically.

Solved

GoogleSignInClient.revokeAccess works. I don't know the reasons why.

 val signInOptions = GoogleSignInOptions.Builder().addExtension(fitnessOptions).build()
            val client = GoogleSignIn.getClient(context, signInOptions)
            client.revokeAccess()
                .addOnSuccessListener {
                    Timber.d("disconnectGoogleFit: onSuccess")
                }

Question

I'd like to ask you what is the diffrences between Fitness.getConfigClient(context, account).disableFit() and GoogleSignInClient.revokeAccess?

Thank you

codingjeremy commented 5 years ago

Comment by jeffreyfhow23 Thursday Jun 20, 2019 at 16:58 GMT


I've tried this but keep getting NullPointerException: Attempt to invoke interface method 'int com.google.android.gms.auth.api.signin.GoogleSignInOptionsExtension.getExtensionType()' on a null object reference

krokyze commented 4 years ago

Hey,

so I was trying to achieve that user can easily log out from previously selected account and just using disableFit() or revokeAcess() didn't work. The only working solution that I found was doing like this:

val fitnessOptions = FitnessOptions.builder()
                .build()

Fitness.getConfigClient(context, GoogleSignIn.getLastSignedInAccount(context))
                .disableFit()
                .continueWithTask {
                    val signInOptions = GoogleSignInOptions.Builder()
                            .addExtension(fitnessOptions)
                            .build()
                    GoogleSignIn.getClient(context, signInOptions)
                            .revokeAccess()
                }
                .addOnFailureListener { e ->
                    // receives error with no message and status code 4
                }

Failure return status code with value 4 which is SIGN_IN_REQUIRED and next time when requesting permissions user is able to select an account.

Shouldn't just using disableFit achieve this result?

brianschrameck commented 4 years ago

We have a similar issue. When a user disconnects the app from their Google Fit account (by going into Google Fit > Profile > Settings > Manage connected apps > [App Name] > DISCONNECT), the GoogleSignIn.hasPermissions call still returns true. Shouldn't it return the current state of the user's profile?

chanx2 commented 4 years ago

We have a similar issue. When a user disconnects the app from their Google Fit account (by going into Google Fit > Profile > Settings > Manage connected apps > [App Name] > DISCONNECT), the GoogleSignIn.hasPermissions call still returns true. Shouldn't it return the current state of the user's profile?

I'm getting the same! Have you solved this?

bschrameckCardioCast commented 4 years ago

I'm getting the same! Have you solved this?

No, I don't think we did. 👎

maonaoda commented 3 years ago

getting the same!

Toubap commented 3 years ago

Same here, dont get why it is not an important issue.. I can see some apps doing some tricks to avoid the situation. Ui side they disconnect the user from google Fit, but they keep the connection in the google fit settings app..

maonaoda commented 3 years ago

@brianschrameck @Toubap you can add below on your AndroidManifest.xml that your app can auto logout from google when you disconnect the app from Google Fit App.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/RevocationBoundService

<service android:name="com.google.android.gms.auth.api.signin.RevocationBoundService" android:exported="true" android:permission="com.google.android.gms.auth.api.signin.permission.REVOCATION_NOTIFICATION" />

brianschrameck commented 3 years ago

@maonaoda

you can add below on your AndroidManifest.xml that your app can auto logout from google when you disconnect the app from Google Fit App.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/RevocationBoundService

Supposedly it's already being included because we use play-services-auth:

This service is included automatically when depending upon com.google.android.gms:play-services-auth. There is no need to include it manually.

AlexKnyazyuk commented 3 years ago

Getting the same, any updates?

MeLean commented 2 years ago

Hi, everyone. I have a similar experience when the app requests three scopes for example

private val scopesArr = arrayOf(
        Fitness.SCOPE_ACTIVITY_READ,
        Fitness.SCOPE_NUTRITION_READ,
        Fitness.SCOPE_BODY_READ
    )

but the user grants permission to only one of them (ticks only one checkbox in the shown activity). In that case when I check for permission like this: GoogleSignIn.hasPermissions(acc, *scopesArr) it returns true

lyw-94 commented 2 years ago

it's year 2022, still the same issue

Edodums commented 1 year ago

It's almost 2023 and still this hasn't been fixed. Guys I know that you're working on Health Connect ( and I really love that , I can't wait to integrate it ), but shouldn't this have to be addressed an fixed. If it has been already been fixed, which version should I use of :

To avoid the problem?

ShirleyGuo1 commented 1 year ago

It's been years still nobody is looking at this critical issue. I'm wondering what is the alternative solution to check if the GoogleAuth is disconnected? GoogleSignIn.requestPermissions every time?

yauheniya-hrynkevich commented 1 year ago

It is July 2023, and issue is still not resolved