Search code examples
androidapioauthgoogle-fit

Google Fit API authentication weirdness


I'm developing some watch faces for Android Wear (minSdkVersion=21 and targetSdkVersion=23) and integrating the Google Fit API for Android.

As you know, to use the Google Fit API for Android in an app, you need an OAuth 2.0 client ID for Android applications. So I went to the API Access page on the Google API Console, created an OAuth Client and a Client ID using the Android Studio (v 2.2.3) debug certificate SHA-1 fingerprint and the package name for my test watch face. I followed all instructions here: https://developers.google.com/fit/android/get-api-key

I then implemented in my watch face the request for permission and authentication steps, the subscribe, the listeners etc. All went well. Authorization were asked and successfully granted, watch face was working, fitness data correctly accessed and displayed.

This was in the morning. Then, suddenly, in the afternoon, fitness data stopped to update on the watch face. Cumulative distance was now showing 0.00 meters while just one minute before it was in perfect sync with the info shown on the phone Google Fit app. And an UNKNOWN_ERROR (5000) started showing in the Android monitor inside Android Studio.

I (clean) rebuilt the watch face, uninstalled from the watch, removed the credentials on the Google API Console and started over. It worked, again. Without changing one single line of code. That left me with an unconfortable sensation. If you're a developer you know what I'm talking about: Code works. But how?

So I tried something different. I disabled the Fitness API in the Console and removed OAuth Client and Client ID for the watch face. I even uninstalled Google Fit from my phone and my watch. Again I uninstalled the watch face from the watch, clean rebuilt and reinstalled. When I started it, it asked for the proper Google Fit API authorization.

And it worked. The watch face authenticates and receive fit data. Without actual OAuth Client and a Client ID! I thought there was some kind of caching in action. But now it's several hours and it keeps working.

Does anybody have any idea WHY? How is this possible?

Thank you!


Solution

  • Turns out the intermittent connection fail and the associated UNKNOWN_ERROR (5000) (Also preceded by "User recoverable auth exception: btx: NeedPermission") shown in logcat, is a bug of Android Wear 2.0 as you may read here: https://code.google.com/p/android/issues/detail?id=233942 Same code is working perfectly in Android Wear 1.5