Skip to main content

Any help greatly appreciated, first attempt at using the Strava API. I’m sure this is staring me in the face but i’ve run out of ideas.

So I can successfully use Postman to GET https://www.strava.com/api/v3/athlete with an Authorization header of {Bearer nnxxnnnxxnnxx….}. Returns my athlete details So far so good 😍

 

However when I try and run the simple Python script in the Strava Developer Docs (below) I replace ‘YOUR_ACCESS_TOKEN’ with any of these variations then I get a 401 Unathorised and an Invalid Access Token error 😥

 

swagger_client.configuration.access_token = 'Authorization: Bearer nnxxnnnxxnnxx….'

or

swagger_client.configuration.access_token = 'Bearer nnxxnnnxxnnxx….'

or

swagger_client.configuration.access_token = 'nnxxnnnxxnnxx….'

 

HTTP response body: {"message":"Authorization Error","errors":"{"resource":"Athlete","field":"access_token","code":"invalid"}]}
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
api_instance = swagger_client.AthletesApi()

try:
# Get Authenticated Athlete
api_response = api_instance.getLoggedInAthlete()
pprint(api_response)
except ApiException as e:
print("Exception when calling AthletesApi->getLoggedInAthlete: %s\n" % e)

 

Am I just making some terrible noob formatting error for the access_token or is there something else i’m missing?

I’m getting the access_token from my API page. I refreshed before I started doing this and it’s not expired, and it works fine on Postman.

 

Well I seem to have finally cracked it, this pattern works and authorizes fine now

api_instance = swagger_client.AthletesApi()
api_instance.api_client.configuration.access_token = 'nnxxnnnxxnnxx….'

The example on the Strava developers documenation still doesn’t work

swagger_client.configuration.access_token = 'YOUR_ACCESS_TOKEN'
api_instance = swagger_client.AthletesApi()

Hopefully this will help the next noob in the house 😎


Reply