Access the Google Fitness API (Sleep Data ) Flutter Firebase login using Facebook Auth?

228 Views Asked by At

We are building a Mobile app using Flutter that reads the Sleep Data and shows a dashboard based on the Sleep Data,

We are using Firebase for Social Login. Currently we support Google, Facebook and Custom Email / Password based login.

Google Sign-in

The app is getting Sleep Data, when the user logins using Google Account. We are passing the Google API scope as shown below.

FirebaseAuth.instance, UserRepository(), GoogleSignIn(scopes:[
'https://www.googleapis.com/auth/fitness.sleep.read',
])),

Facebook Sign-in

If the user logins using Facebook Account, we are getting the below error. Not sure, how to pass Google API scope in Facebook Login. I am getting the below issue:

W/FLUTTER_HEALTH::ERROR(31643): There was an error getting the sleeping data!
W/FLUTTER_HEALTH::ERROR(31643): 4: The user must be signed in to make this API call.
W/FLUTTER_HEALTH::ERROR(31643): [Ljava.lang.StackTraceElement;@7eb1aa0
W/FLUTTER_HEALTH::ERROR(31643): There was an error getting the sleeping data!
W/FLUTTER_HEALTH::ERROR(31643): 4: The user must be signed in to make this API call.
W/FLUTTER_HEALTH::ERROR(31643): [Ljava.lang.StackTraceElement;@ef740cc
W/FLUTTER_HEALTH::ERROR(31643): There was an error getting the sleeping data!
W/FLUTTER_HEALTH::ERROR(31643): 4: The user must be signed in to make this API call.
W/FLUTTER_HEALTH::ERROR(31643): [Ljava.lang.StackTraceElement;@abd9db8

Please take a look at the code we are using:

`Future fetchSleepData() async {
     List<HealthDataPoint> _healthDataList = [];
    final types = [
      HealthDataType.SLEEP_ASLEEP,
      HealthDataType.SLEEP_AWAKE,
      HealthDataType.SLEEP_IN_BED
    ];

    final permissions = [
      HealthDataAccess.READ,
      HealthDataAccess.READ,
      HealthDataAccess.READ
    ];
    final now = DateTime.now();
    final yesterday = now.subtract(Duration(days: 10));
    HealthFactory health = HealthFactory();
    bool requested =
        await health.requestAuthorization(types, permissions: permissions);
    await Permission.activityRecognition.request();
    await Permission.location.request();
    if (requested) {
      try {
        // fetch health data
        List<HealthDataPoint> healthData =
            await health.getHealthDataFromTypes(yesterday, now, types);
        // save all the new data points (only the first 100)
        _healthDataList.addAll((healthData.length < 100)
            ? healthData
            : healthData.sublist(0, 100));
      } catch (error) {
        print("Exception in getHealthDataFromTypes: $error");
      }
      // filter out duplicates
      _healthDataList = HealthFactory.removeDuplicates(_healthDataList);
      // print the results
      _healthDataList.forEach((x) => print(x));
    }
  }`

Please help me to read Sleep Data from Google API using Flutter Firebase. The User is logged in using Facebook Auth.

Any advice would be appreciated.

1

There are 1 best solutions below

0
On

Google services cannot be accessed with a Facebook auth. Google auth is required for this purpose. Use Google Auth as your primary sign-in method, and use Facebook Auth only if necessary for what purpose you need it for.