Accessing Strava Data via API

I've set up the API with Strava and can now directly access current data.

In my Geo Activity Playground project I have implemented a few things like summary statistics. Using third-party code I have also added a heatmap feature. This is pretty nice.

The issue is that it currently works off a download of all my data from Strava. That is a ZIP file where everything is contained. The problem is that obtaining it requires multiple manual steps and also I have to download everything that is there.

I wanted to use the API to download my data incrementally and do stuff with it. Using a Towards Data Science tutorial one can learn a bit more.

Setting up the API key

First one needs to create a new app on the Strava Settings page. Then one needs to access https://www.strava.com/oauth/authorize?client_id=…&redirect_uri=http://localhost&response_type=code&scope=activity:read_all,activity:write in order to get a token. After authorizing the app with my account I get redirected to http://localhost/?state=&code=…&scope=read,activity:write,activity:read_all from which I can read the token. I don't need to set up a server there.

This then gives me code which can be used once to obtain an access token. For this request https://www.strava.com/oauth/token?client_id=…&client_secret=…&code=…&grant_type=authorization_code and one will retrieve a result:

{
    "token_type": "Bearer",
    "expires_at": 1690552714,
    "expires_in": 21600,
    "refresh_token": "…",
    "access_token": "…",
    "athlete": {
        "id": 0,
        "username": "rkowlshjf",
        "resource_state": 2,
        "firstname": "…",
        "lastname": "…",
        "bio": "",
        "city": "…",
        "state": "Nordrhein-Westfalen",
        "country": "…",
        "sex": "…",
        "premium": false,
        "summit": false,
        "created_at": "2019-01-03T16:06:06Z",
        "updated_at": "2023-07-24T07:48:41Z",
        "badge_type_id": 0,
        "weight": 0.0,
        "profile_medium": "…/medium.jpg",
        "profile": "…/large.jpg",
        "friend": null,
        "follower": null
    }
}

One needs to keep the access_token as well as the refresh_token.

Fortunately one doesn't need to wrap the API in any way as there is already stravalib out there with great documentation. This way one can access the data.

After having this done, I can now access my Strava data and do all sorts of things with it.