William Chou
William Chou

Reputation: 772

Using firebase google auth and google fit api with single OAuth2 token

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?

Upvotes: 2

Views: 2379

Answers (1)

William Chou
William Chou

Reputation: 772

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.

Upvotes: 3

Related Questions