2

I am currently working on a simple web app that will make an GET call to a user's google fit data.

To reduce the complexity of the app, I plan to host it on firebase and take full advantage of cloud functions, firestore and the authentication tools.

So far, I have managed to read my google fit data using googleapis node library on the frontend by passing the fitness scope https://www.googleapis.com/auth/fitness.activity.read as part of the google sign in button.

If I were to use firebase's google auth implementation, this would have a different client ID / different scopes than the credentials I made for reading the google fit data.

It seems if I want to use firebase AND google fit, the user would have to login using firebase's google auth so I can authenticate database writes to my app, and also grant me access to their google fit data from within the app.

If there a way this could be combined so I could use a single token to authenticate and read google fit data?

William Chou
  • 752
  • 5
  • 16

1 Answers1

3

Looks like I just had to dig a bit deeper, you can add scopes to your google auth provider instance.

https://firebase.google.com/docs/auth/web/google-signin

var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/fitness.activity.read');

firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
})

Then to make the fitness reading, use the access token returned in the Authorization header

Authorization: `Bearer ${accessToken}`
axios.post('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate?alt=json', data, axiosconfig)

Note: If you want to do background requests to a users data while they are not in the app (offline in oauth2 terms), you will need to use firebase in conjunction with gapis js library.

William Chou
  • 752
  • 5
  • 16
  • Excellent answer, very confusing documentation. Did you ever figure out another way to retrieve the access token? Because when you are already logged in, you don't go through this flow as far as I can see. And it will also be refreshed over time as well – atlmag Jul 31 '20 at 10:25
  • 1
    One thing I learned when dealing with google api is to use their client libraries. It will abstract away the access token and refresh token logic for you. What exactly are you trying to accomplish? I can try to point you in the right direction. – William Chou Aug 04 '20 at 20:57
  • I just playing with it for now, but I want to i.e list out "all runs in the last week" for current user. I didn't think of looking for an client lib for Google Fit, let me see if there are any. Thanks :) – atlmag Aug 06 '20 at 06:49
  • 1
    it would just be their official library with thge google fit scopes you want: https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow – William Chou Aug 06 '20 at 14:02
  • @WilliamChou Could you explain a little more about those client libraries? im developing an app and trying to store the google token but i bumped in this very same problem of it having a refresh time (my problem: https://stackoverflow.com/questions/70673275/integrate-google-fit-api-with-react-native) – Pedro Fontes Jan 11 '22 at 20:59