Skip to main content
Solved

Python - Trying to get all stats using API - URL showing an error


Forum|alt.badge.img+3

Wondering if someone could help - I'm trying to obtain totals for an athlete.  I have managed to get all activities, but would also like totals.

This is the following code I am using:

stats_url = f"https://www.strava.com/api/v3/athletes/{client_id}/stats?" f"access_token={access_token}" statsresponse = requests.get(stats_url) stats = statsresponse.json() print('Stats API:', stats_url) ride_total_year_calculated = round(stats['ytd_ride_totals']['distance']/1000,1 )

 

When copy and pasting the URL I get the following response:

 

{"message":"Forbidden","errors":[]}


 I'm obviously doing something really stupid here, but can't see it right now!

 

For further info, the following works:

activities_url = f"https://www.strava.com/api/v3/athlete/activities?" f"access_token={access_token}" print('RESTful API:', activities_url) response = requests.get(activities_url, params=params) print(len(response.json())) for i in range(0,len(response.json())): activity = response.json()[i] print('Disance:', activity['distance'], 'm') print('Date:', activity['start_date']) print('type:', activity['type'])

 

Best answer by ActivityFix

stats_url = f"https://www.strava.com/api/v3/athletes/{client_id}/stats?" 
            f"access_token={access_token}"

Did you mean to insert client_id there? That should be athlete_id

See: https://developers.strava.com/docs/reference/#api-Athletes-getStats

GET /athletes/{id}/stats

Parameters

id
required Long, in path
The identifier of the athlete. Must match the authenticated athlete.

 

View original
Did this topic help you find an answer to your question?

3 replies

ActivityFix
Superuser
Forum|alt.badge.img+24
  • Superuser
  • 232 replies
  • Answer
  • January 23, 2024
stats_url = f"https://www.strava.com/api/v3/athletes/{client_id}/stats?" 
            f"access_token={access_token}"

Did you mean to insert client_id there? That should be athlete_id

See: https://developers.strava.com/docs/reference/#api-Athletes-getStats

GET /athletes/{id}/stats

Parameters

id
required Long, in path
The identifier of the athlete. Must match the authenticated athlete.

 


Forum|alt.badge.img+3
  • Author
  • Hub Rookie
  • 2 replies
  • January 24, 2024

That's a good point - however that is set to my athlete ID.  The URL looks like this (with x's as actual athlete id):

 

https://www.strava.com/api/v3/athletes/xxxxxx/stats?access_token=2ac758f65d08641b17791d6c69ed1a708407b1ca


Forum|alt.badge.img+3
  • Author
  • Hub Rookie
  • 2 replies
  • January 24, 2024

You’re absolutely right, thank you so much 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept, you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings