google-services icon indicating copy to clipboard operation
google-services copied to clipboard

GoogleSignIn with FitnessOptions throws wierd ApiException 5000

Open RudolfHladik opened this issue 8 years ago • 11 comments

GoogleSignIn with FitnessOptions

Step 2: Describe your environment

  • Device: Nexus5x, Nexus5, GalaxyS7 and several more devices
  • OS version: Android 5+ maybe older are affected too
  • SDK version:
implementation "com.google.android.gms:play-services-fitness:11.8.0"
implementation "com.google.android.gms:play-services-auth:11.8.0"
  • [Android] Google Play Services version: 11.9.51 (440-177350961)

Step 3: Describe the problem:

Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedAccount(context))
        .readData(dataReadRequest)

end with ApiException with FitnessStatusCode 5000 (https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessStatusCodes.html#API_EXCEPTION) even though I have had requested all needed permissions and user has approved my request and status.startResolutionForResult(..) cannot be called because resolution == null

Steps to reproduce:

  1. download my library with example https://github.com/thefuntasty/RxGoogleFit.git (master branch)
  2. create new google account (gmail)
  3. with that new account signed in run example app(fittester) from repo in step 1
  4. click on GET DATA in app
  5. select newly created google account and approve all permissions
  6. app should print Statuses for all read requests and some insert requests, instead it throws ApiException

Observed Results:

  • What happened?

in ResolutionActivity.onActivityResult after all granted perms I try to get data with tryGetData() which is successful but same request in HistoryReadSingle with same Account results in ApiException 5000

this happens only on newly created google accounts or on account where no fit permissions have been requested yet

the most strange thing is, when I close app from last used apps and run it again and click GET DATA everything works

Expected Results:

when I check GoogleSignIn.hasPermisions(account, signInOptionsExt) and it returns TRUE, I would expect that I can request data via Fitness.getHistoryClient(context, account).readData(..) without any ApiException just like it behaves when I close app and start it again or when there is statusCode 5000 there should be resolution too as documentation says

Relevant Code:

// code here to reproduce the problem
https://github.com/thefuntasty/RxGoogleFit.git (master branch)

RudolfHladik avatar Jan 22 '18 17:01 RudolfHladik

Can you try replacing GoogleSignIn.getLastSignedInAccount() with the method GoogleSignIn.getAccountForExtension(): https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignIn.html

That method will get an account with the proper scopes, if it is available. If that method returns null you need to do requestPermissions (as you're already doing elsewhere I think)

samtstern avatar Jan 23 '18 19:01 samtstern

Hi @RudolfHladik , I looked at your code at https://github.com/thefuntasty/RxGoogleFit.git, I don't see any code getSignInIntent() which help you with OAuth consent. The only place you check OAuth permission is in isPermsGranted() function and its result is never consumed.

https://github.com/thefuntasty/RxGoogleFit/blob/efc119aa6159971f6116ef3f19e9c29685d450e6/example/src/main/java/com/funtasty/fittester/ui/main/MainActivity.kt#L37

BTW, irrelevant to your issue, please use GoogleSignIn.getAccountForExtension whenever you try to get a Fitness client.

isaisachen avatar Jan 23 '18 19:01 isaisachen

@samtstern thank you for your answer. I've implemented the method you suggested, but the issue unfortunately remains the same. I have made changes to the repo, you can check if I have not misunderstood your guidance. Sad thing is, that when I googled getAccountForExtension() method, i've got only one result, the reference in GoogleSignIn documentation. No mention in Fitness documentation. But I also understand that the api is changing fast.

@isaisachen thank you also for your answer. Method MainActivity.isPermGranted() has informative character (Log). Your suggested method getSignInIntetn I've also tried, but with same result as the issue describes. The use is in ResolutionActivity.getAccount() as it is written in Kotlin it is hidden in signInClient.signInIntent == signInClient.getSignInIntent()

RudolfHladik avatar Jan 26 '18 10:01 RudolfHladik

@RudolfHladik it still looks like you're ignoring the result of isPermsGranted() here? https://github.com/thefuntasty/RxGoogleFit/blob/master/example/src/main/java/com/funtasty/fittester/ui/main/MainActivity.kt

samtstern avatar Jan 26 '18 20:01 samtstern

@RudolfHladik Did you get fix this?

pawnjester avatar Jun 27 '19 09:06 pawnjester

same problem. Do u solve it?

Tooto avatar Dec 12 '19 09:12 Tooto

same problem. @Tooto @pawnjester @RudolfHladik have you guys got any solution? thanks

brjichkar avatar May 18 '21 07:05 brjichkar

I have the same problem, now my app completely broken

dzungpv avatar May 26 '21 04:05 dzungpv

I have the same problem, now my app completely broken

The problem is with Permission. YOu should have matching permission. My isseu was, even though I did requested for Fitness Read and Write application, Google was not considering my write permission and was throwing error, hence I removed my write request from app and used only read request, and app started to work. Note here- on Google Play console I allowed both scope Activity.Fitness.Read and activity.fitness.write but somehow, in development of app, Google was only allowing me to use read scope. might be a bug in current sdk

brjichkar avatar May 29 '21 13:05 brjichkar

I have the same problem, now my app completely broken

The problem is with Permission. YOu should have matching permission. My isseu was, even though I did requested for Fitness Read and Write application, Google was not considering my write permission and was throwing error, hence I removed my write request from app and used only read request, and app started to work. Note here- on Google Play console I allowed both scope Activity.Fitness.Read and activity.fitness.write but somehow, in development of app, Google was only allowing me to use read scope. might be a bug in current sdk

I have fix the problem: update to SDK 20.0.0 and request both read and write permission. But I think that there is a bug, on the consent screen and docs they say FitnessOptions.ACCESS_WRITE permission can write and read app own data, but not could not read itself data, unless request READ permission. More info https://developers.google.com/fit/improvements

dzungpv avatar May 29 '21 16:05 dzungpv

I just Comment out this line addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE) and update both the library implementation 'com.google.android.gms:play-services-fitness:20.0.0' implementation 'com.google.android.gms:play-services-auth:19.0.0' and uninstall the app and again install works fine

narendramendios avatar Jun 07 '21 12:06 narendramendios