A Strava dashboard on a Raspberry Pi (Part 3): The Strava API

This is part 3 of a series of blogposts on how I created a Strava dashboard on a Inky Impression e-ink display with a Raspberry Pi.

OAuth2

This was the part that I expected to be the hard part: getting my data from Strava. Or, to be more precise: getting the connection right so the Strava API would allow me to get that data. Because it requires authentication via the OAuth2 protocol and I’ve tried a similar thing a few years back with a Google API and I just didn’t get it. But now I do.

Strava API documentation

It requires a whole “dance” between your computer code and the Strava API where you exchange all kinds of tokens back and forth. Strava’s Getting Started with the Strava API document explains it quite well. And this blogpost by Graziano Fuccio helped me a lot with the Python code: http://www.grace-dev.com/python-apis/strava-api/.

Frustratingly I still didn’t get it to work though. The reason I found out, is because the URL of the authentication has changed. From https://www.strava.com/oauth/token it became  https://www.strava.com/api/v3/oauth/token. I found this elsewhere in the Stava API documentation, where the correct URL was shown. I’ve told Strava that their Getting Started documentation is outdated. They asked me to create a ticket and I’ve done so, but I don’t think they changed their document yet. But Graziano Fuccio did though.

My code and process

I’ve used Graziano Fuccio’s Python code for my Strava Inky Dashboard and changed it a little. My code can be downloaded from Github: https://github.com/Marcel-Jan/StravaInky

This is how my current process is:

1. Install Pillow

Actually this is not for authentication, but to show stuff on the Inky Impression display. But if you are going to run stravainky_dashboard.py, you’re going to need this Python library.

2. Create your Strava API application

Now we’re going to create a Strava API application. For this go to the Strava API Application page: https://www.strava.com/settings/api. Create an application.

Now you’ll get a form that looks something like this. Sorry that this screenshot is in Dutch. I don’t know how to make Strava show it in English.

You’ll be asked to give it a name, a category (I chose visualisation, but I’m not sure it matters). I don’t think the website URL matters that much for this. Authentication of the callback domain will later appear in an URL that is generated. “localhost” will be fine here.

After that was successful, you will get something like this (except maybe not in Dutch):

3. Download my code from Github

The code can be downloaded from here: https://github.com/Marcel-Jan/StravaInky.

4. Edit config.py

Because the secrets and tokens used to authenticate your Strava API application are rather sensitive, I decided to create a separate config.py file where I store client id, client secret (and later the refresh token).

Copy the Client ID and Client Secret from the API application details (screenshot above) and paste them in config.py in client_id and client_secret.

5. Run stravainky_getaccesstoken.py

Okay, this is the part where you need to pay close attention. If all goes well, you only need to run stravainky_getaccesstoken.py only once.

When you run it, it will produce an URL and it will ask you for a code.

You get the code by copying the URL (after the “Click here” part) and go to that URL in a browser, you’ll get a page where you need to authorise your app. Of course we want to do that.

And here’s the strange thing: when you click Allow (or Toestaan in Dutch), you’ll be sent to a URL that goes nowhere. It will look like things went wrong, but in the URL is your refresh token that you need to copy.

Now go back to the terminal where you started stravainky_getaccesstoken.py and paste that refresh token (“Insert the refresh token from the url”).

This will put the access token and other details in a file called strava_tokens.json. If strava_tokens.json contains errors, check that you entered the correct client id and client secret in config.py and run this step again.

6. Enter refresh token in config.py

Now that you have the refresh token. Also enter it in config.py:

7. Run stravainky_dashboard.py

You should now be able to get data from the Strava API with Python. stravainky_dashboard.py gets this data and displays it on Inky Impression display.

It might have a Christmas theme, like this one. The images can be found in the images directory.

The Strava dashboard on the Inky Impression display with a Christmas theme.

Troubleshooting

Nothing ever goes right the first time. What to check when things go wrong?

Check strava_tokens.json

If it contains errors, check that you put in the correct client id and client secret in config.py.

Check the log

stravainky_dashboard.py writes a log file called stravainky_dashboard.log that might contain some error messages.

In the next blogpost we will look at how to draw your own dashboard on the Inky Impression with the Pillow library.

This entry was posted in Howto, Python and tagged , , , , , . Bookmark the permalink.

7 Responses to A Strava dashboard on a Raspberry Pi (Part 3): The Strava API

  1. Pingback: Building a Strava dashboard on a Raspberry Pi with an e-ink display | Expedition Data

  2. Pingback: A Strava dashboard on a Raspberry Pi (Part 2): Installing software | Expedition Data

  3. Mike Parsons says:

    Hi, Brilliant tutorial, I’m trying to do the same think with an InkyFrame, which has a Pi Pico attached – MicroPython code. It’s close, but too far for me.

    Do you think it’s possible?

Leave a Reply

Your email address will not be published. Required fields are marked *