Search code examples
react-nativegoogle-fit

How to connect two different React Native apps to GoogleFit?


I'm working on a React Native project which was set up by someone else; the project connects to Google Fit on the user's device to read some of the health data.

The code we have is used to build two different apps, both with the same code base, but with slightly different branding, features etc. In App1 (the main app), accessing Google Fit is fine - the authorisation screen is shown if necessary (ie the screen that asks me to select an account to continue to App1), and if the user allows data access, then the data is read from the device. However, when I try to do the same with App2, the authorisation screen is shown, and I can select an account, but then the system seems to believe that I've denied access, and no data is read.

The 2 Apps have different names and display names in app.json; they also have different Gradle files which allow them to connect to different back ends, show different text etc. They are set up as different apps in the Firebase console, so they have different google-services.json files. App 1 has its own GCP project, but App 2 doesn't - does that matter? Apart from this, everything else about the apps is the same.

The code I have for connecting with Google Fit is as follows (this function is triggered when the user taps a button labelled 'Link Google Fit'). This uses the package react-native-google-fit (https://github.com/StasDoskalenko/react-native-google-fit).

if (!GoogleFit.isAuthorized) {
  const options = {
    scopes: [Scopes.FITNESS_HEART_RATE_READ],
  };
  GoogleFit.authorize(options)
    .then((authResult) => {
          // AT THIS POINT AUTH RESULT IS:
          // {"message": "Authorization cancelled", "success": false}
    })
};

So even when I select a valid account to use, I get 'Authorization cancelled'.

I assume there's maybe something that has to be enabled to allow App 2 to use Google Fit, but I don't know what it is.


Solution

  • Google is very strict when it comes to different brands using the OAuth services. What you want to have, or rather, what your client should have, are separate projects for each brand.

    You can go over this FAQ Section for more details.

    This is so that they can properly verify each project, especially when they have to use restricted scopes.