# Thunkable integrating with google fit

**URL:** <https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679>\
**Category:** Questions about Thunkable X\
**Created:** [September 2, 2022, 7:14am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679 "2022-09-02T07:14:13Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 10:34am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/21 "2022-09-04T10:34:03Z")

</div>

In the `Request URL` type the following to get the list of Data Sources

```auto
https://www.googleapis.com/fitness/v1/users/me/dataSources

```

You can then choose what Data Source you want to use and then use the first URL to get the data.

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 10:54am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/22 "2022-09-04T10:54:17Z")

</div>

i got the results now which one is the data source?

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/d/c/dc890421828ab15106811299fa81aef49db91f0d.png)

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 10:59am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/23 "2022-09-04T10:59:32Z")

</div>

I circled it for you

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/e/4e698488ca60c18700e1ac2534e7c8adacb4381b.png)

Every type of data has its own source. It seems you have selected only this scope so you are getting only one source.

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 11:04am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/24 "2022-09-04T11:04:35Z")

</div>

thanks for circling it

and i only want to read the device steps so i selected only one scope  
and how would i fetch the users’s datasource?

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 11:12am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/25 "2022-09-04T11:12:30Z")

</div>

Now you can use the data source ID in the original URL

```auto
https://www.googleapis.com/fitness/v1/users/me/dataSources/[[use the data source obtained from previous API call]]/datasets/1662166801000000-1662339600000000

```

This will extract the data of yesterday and today.

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 11:22am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/26 "2022-09-04T11:22:58Z")

</div>

ok now will i put this url in the webapi?

[https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.active\_minutes:com.google.android.fit:Xiaomi:Redmi%20Note%207S:1cb0a75f:top\_level/datasets/1662166801000000-1662339600000000](https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.active_minutes:com.google.android.fit:Xiaomi:Redmi%20Note%207S:1cb0a75f:top_level/datasets/1662166801000000-1662339600000000)

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [September 4, 2022, 11:37am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/27 "2022-09-04T11:37:14Z")

</div>

Are you already grabbing the access token

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 11:39am UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/28 "2022-09-04T11:39:58Z")

</div>

what?

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [September 4, 2022, 12:43pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/29 "2022-09-04T12:43:35Z")

</div>

My guess is that this api requires user authentication via. An oauth 2.0 flow. Did you set that part up yet?

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 1:05pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/30 "2022-09-04T13:05:09Z")

</div>

Yes, @jared this API requires OAuth2.0 to work properly.

The next step is to get OAuth2.0 client ID and Secret ID and exchange it with as Access Token to make it work.

The idea from using the Google Playground is to test and document the steps required by the API because the Playground automatically resolves an Access Token to use with the API.

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [September 4, 2022, 1:07pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/31 "2022-09-04T13:07:16Z")

</div>

The easiest way to set this up is to direct somebody to a website where you authenticate with Google. Your need to pass along some meaningful user identifier in the process to the website. Then on the website you could authenticate with Google, get the auth code, grab the access token with the code, and then post that value to the users object on whatever database the user object lives in.

As such, this would give you access within the app to the UsERS access token at which point you could continue to refresh the top and keeping it valid indefinitely until the user revokes the token. Once you have the token and are keeping it alive when needed, you’ll be able to make all the authenticated calls that you need to,

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 4:03pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/32 "2022-09-04T16:03:11Z")

</div>

No, i think  
But i have a auth webpage

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 4:04pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/33 "2022-09-04T16:04:22Z")

</div>

> [@muneer](#):
>
> e the Playground automatically resolves an Access Token to use w

ok i have oauth client id

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/b/9be67e0f5b793a8fe235be0b777cee13b9d0e2a6.png)

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 4:04pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/34 "2022-09-04T16:04:51Z")

</div>

ok but how to set it up  
i have a login website

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 4:48pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/35 "2022-09-04T16:48:17Z")

</div>

> [@jared](#):
>
> Your need to pass along some meaningful user identifier in the process to the website. Then on the website you could authenticate with Google, get the auth code, grab the access token with the code,

What you say @jared is valid but to this specific use case there is a shortcut. The service intended for this app is a **Google Fit API** which is a Google service and therefore you can use the **Google Consent Server** as the Redirect URL. There is no need to pass it through a website in this case.

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [September 4, 2022, 5:07pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/36 "2022-09-04T17:07:41Z")

</div>

@muneer correct me if i’m wrong here. are you saying you dont need the _ **users** _ access token to get this info? or that the app doesnt have to authenticate as the user to grab the users info?

@lolxdyt6950as I would use Google Cloud functions to handle all the sensitive info processing

after authentication the user on my webpage and redirecting back, i’d pass the code to the following function

```auto
exports.returnAccessToken = functions.https.onRequest(async (req, res) => {
  const cors = require("cors")({origin: true});
  cors(req, res, () => {
    // your function body here - use the provided req and res from cors
    const axios = require("axios");
    const qs = require("qs");

    const data = qs.stringify({
      "code": `${req.query.code}`,
      "client_id": "",
      "client_secret": "",
      "redirect_uri": "https://jared-gibb-project-123.web.app/",
      "grant_type": "authorization_code",
    });
    const config = {
      method: "post",
      url: "https://www.googleapis.com/oauth2/v4/token",
      headers: {

        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: data,
    };

    axios(config)
        .then(function(response) {
          console.log(JSON.stringify(response.data));
          res.json(response.data);
        })
        .catch(function(error) {
          console.log(error);

          res.json(error);
        });
  });
});

```

refresh as needed here

```auto
exports.refreshAccessToken = functions.https.onRequest(async (req, res) => {
  const cors = require("cors")({origin: true});
  cors(req, res, async () => {
    // your function body here - use the provided req and res from cors
    const axios = require("axios");
    const qs = require("qs");
    const data = qs.stringify({
      "client_id": "",
      "client_secret": "",
      "refresh_token": `${req.query.refresh_token}`,
      "grant_type": "refresh_token",
    });
    const config = {
      method: "post",
      url: "https://www.googleapis.com/oauth2/v4/token",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: data,
    };
    const response = await axios(config);
    res.json(response);
  });
});

```

and save the data to the user so they can do this at any time in the future

```auto
exports.saveAccessTokenToUser = functions.https.onRequest(async (req, res) => {

  const uid = req.query.uid;
  const access_token = req.query.access_token;
  const refresh_token = req.query.refresh_token;
  const cors = require("cors")({origin: true});
  cors(req, res, async () => {
    //get a referene to the database
    const db = admin.firestore();
    //get a reference to the user document
    const userRef = db.collection("users").doc(uid);
    //save the access token to the user document
    await userRef.update({
      access_token: access_token,
      refresh_token: refresh_token
    });
    res.json({message: "success"});

  })
});

```

and an HTML page to handle passing around the data from app to backend PLUS the authentication component  
[https://jared-gibb-project-123.web.app/?state=null&code=4/0AdQt8qjb-CEljf\_4GL1sheE-gjL43rYwzN3n92bsw1jkYwlSG837qg0K5MIKyvTsWN1fdA&scope=email%20profile%20https://www.googleapis.com/auth/calendar%20openid%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&authuser=0&prompt=consent](https://jared-gibb-project-123.web.app/?state=null&code=4/0AdQt8qjb-CEljf_4GL1sheE-gjL43rYwzN3n92bsw1jkYwlSG837qg0K5MIKyvTsWN1fdA&scope=email%20profile%20https://www.googleapis.com/auth/calendar%20openid%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&authuser=0&prompt=consent)

and the app of course to start it all of is needed. this is only an idea  
[https://x.thunkable.com/projects/61e4e200e0ca1b001138c8b3/3ea7620c-96d0-4249-90e2-04d53f2cd904/blocks](https://x.thunkable.com/projects/61e4e200e0ca1b001138c8b3/3ea7620c-96d0-4249-90e2-04d53f2cd904/blocks)

The real implementation is only a bit more involved here. you’d need a firebase login page or some other auth provider.

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 5:09pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/37 "2022-09-04T17:09:27Z")

</div>

i have a firebase login page  
with google and facebook [both not setted up since its a source code and i can enable it anytime]

[https://kwiply.online](https://kwiply.online)

its only email here

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [September 4, 2022, 5:11pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/38 "2022-09-04T17:11:53Z")

</div>

You should have them log in from the app, direct to your google auth login so you can get permissions handled. pass their uid as a state during auth. then you can pass the uid and the code to your server for processing

---

<div class="post-metadata">

**Author:** ![lolxdyt6950as](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lolxdyt6950as](https://community.thunkable.com/u/lolxdyt6950as)\
**Post date:** [September 4, 2022, 5:13pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/39 "2022-09-04T17:13:36Z")

</div>

ok

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [September 4, 2022, 5:26pm UTC](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679/40 "2022-09-04T17:26:44Z")

</div>

> [@jared](#):
>
> are you saying you dont need the _ **users** _ access token to get this info? or that the app doesnt have to authenticate as the user to grab the users info?

No. I’m just saying that instead of creating HTML page to handle codes use the `Google Consent Server` to do it instead. This would mean one thing less to worry about.

You will still have to do the dance of authentication and token exchange though.

[Previous page](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679.md?page=1)

[Next page](https://community.thunkable.com/t/thunkable-integrating-with-google-fit/2024679.md?page=3)
