Authenticate with the Strava API using stravalib#
To retrieve data from the Strava API, you must first authenticate with Strava. This page provides you with the information required to set up authentication with Strava.
Step 1: Create an application in your Strava account#
First, create a new application in your Strava account. To do this:
Login to your Strava account
Go to settings –> My API Application
Create a new application in your account
The above Strava documentation page image shows the Strava API create application page. The website value in this image is “localhost.” This value can be any if you authenticate to gain local access to your Strava data. But if you plan to build a web application, you should place the URL of your application in that field.#
More resources on setting up a Strava app
If you want a more technical overview, see the official Strava documentation
Step 3. Refresh your token#
A token is valid for 6 hours. After that time period, you need to refresh it if you want to continue to interact with the Strava API. There are two ways that you can do this:
You call the
stravalib.client.Client.refresh_access_token()method.You can setup your environment and instantiate the Client object with credentials needed for stravalib to manage to refresh the token for you.
Refresh your token manually#
To refresh your token manually, use the stravalib.client.Client.refresh_access_token() method.
Tip
See the strava-oauth directory for an example of a Flask application that fetches a Strava authentication token.
Setup stravalib to auto-refresh your token#
To enable stravalib to manage the token refresh process, setup two things:
Store the token response that you got above from the initial authentication step. This information should include the:
access_tokenvaluerefresh_tokenvalueexpires_atvalue
Setup your environment with your
client_idandclient_secretlike this:
CLIENT_ID=123445
CLIENT_SECRET=secretvalueshere123
CLIENT_ID should always be an integer. CLIENT_SECRET is always a string.
Once you have setup the client_id and client_secret values in your environment, one way to retrieve them is to use the python_dotenv package.
The workflow will look something like this:
pip install python-dotenv
# In this example you have stored your token refresh data in a `.json` file
import json
import os
from dotenv import load_dotenv
from stravalib import Client
# Open and access the toke_refresh data
# You will populate this when you instantiate a client object below
json_path = os.path.join("path", "to", "json")
with open(json_path, "r") as f:
token_refresh = json.load(f)
# Read the CLIENT_ID and CLIENT_SECRET
load_dotenv()
print("Expires at", token_refresh["expires_at"])
# Instantiate a client object, including your access_token, refresh_token, and token_expires values
# These values, if available, will allow stravalib to check if it needs to refresh the token for you when it makes an API call using the client object
client = Client(
access_token=token_refresh["access_token"],
refresh_token=token_refresh["refresh_token"],
token_expires=token_refresh["expires_at"],
)
athlete = client.get_athlete()
print(f"Hi, {athlete.firstname} Welcome to stravalib!")
print(client.token_expires)
Congratulations! You now know how to authenticate with the Strava API using stravalib.