401 Authorization Error

1,085 views
Skip to first unread message

Paul Carron

unread,
Jun 4, 2018, 4:34:51 AM6/4/18
to Strava API
I've copied this sample code from the Strava API page:

Enter code here...
from __future__ import print_function
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 = 'MY TOKEN'

# create an instance of the API class
api_instance = swagger_client.ClubsApi()
id
= <MY CLUB> # Integer | The identifier of the club.
page = 56 # Integer | Page number. (optional)
perPage = 56 # Integer | Number of items per page. Defaults to 30. (optional) (default to 30)

try:
   
# List Club Activities
    api_response = api_instance.get_club_activities_by_id(id, page=page, per_page=perPage)
    pprint
(api_response)
except ApiException as e:
   
print("Exception when calling ClubsApi->getClubActivitiesById: %s\n" % e)

When I run it I get this response:
Exception when calling ClubsApi->getClubActivitiesById: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Mon, 04 Jun 2018 08:04:52 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Via': '1.1 linkerd', 'Status': '401 Unauthorized', 'X-Request-Id': 'ac85febd-9ee4-4dd1-aeb9-a5c1cec0fafc', 'X-FRAME-OPTIONS': 'DENY', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block'})
HTTP response body: {"message":"Authorization Error","errors":[{"resource":"Application","field":"","code":"invalid"}]}

I'm pretty sure the token is correct. If I go to the Oauth Authorization Page and click Authorize, I get the "It Works!" message. Am I missing something?

Dirk Louw

unread,
Jun 4, 2018, 4:40:25 AM6/4/18
to Strava API
Hi,

So have you tried doing the same calls through postman? What I ran into. I first needed todo the GET, to receive a token. Then you have todo a POST with the token to get an AccessToken like:

URI uri = new URI("https://www.strava.com/oauth/token");
URL url = uri.toURL();

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

try {
StringBuilder sb = new StringBuilder();
sb.append("client_id=" + CLIENT_ID);
sb.append("&client_secret=" + stravaScret);
sb.append("&code=" + accessToken);

conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(sb.toString().getBytes("UTF-8"));
.......
......


Then you receive a access code which you then need to use along side your client_Secerte to do calls. i.e. getClubActivitiesById

Hope this helps

Paul Carron

unread,
Jun 4, 2018, 5:20:33 AM6/4/18
to Strava API
Hi Dirk,

Yes, I can get the data with Postman.

I've noticed that swagger_client/configuration.py has the following line:

self.access_token = ""
Enter code here...

I thought this line in my program wouldn have updated it with the correct token:
swagger_client.configuration.access_token = 'MY TOKEN'

However, that doesn't seem to happen. If I hard code my token into the line in configuration.py I can get some data to return.

Dirk Louw

unread,
Jun 4, 2018, 5:24:01 AM6/4/18
to Strava API
Sounds stupid,

But you are posting you accessToken there and not the token you received back from the first GET call?

Just trying to troubleshoot your problem

Regards

Paul Carron

unread,
Jun 4, 2018, 5:48:01 AM6/4/18
to Strava API
Yes. I thought they were the same thing.

I'll try what you suggested.

Cheers
Paul

Dirk Louw

unread,
Jun 4, 2018, 6:30:44 AM6/4/18
to Strava API
yes, there is your problem. I had that issue as well in the beginning. The token you receive back after the first call is just a temp code to get an accessToken which you can use to make calls on behave of that athlete.

Good luck.

Jon Moon

unread,
Oct 6, 2018, 6:00:21 AM10/6/18
to Strava API
I've just been through this and it seems as though the examples are simply buggy - I had to set the access token on the api instance which was created - 

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

# Configure OAuth2 access token for authorization: strava_oauth
api_instance.api_client.configuration.access_token = MY_ACCESS_TOKEN 
api_instance.api_client.configuration.debug = True

Under the covers each new API instance gets a new configuration object, rather than inheriting from the values set via swagger_client.configuration.

Alternatively I think ActivitiesApi can take an argument to a configuration object in its constructor.

HTH,
Jon
Reply all
Reply to author
Forward
0 new messages