stravalib icon indicating copy to clipboard operation
stravalib copied to clipboard

AccessUnauthorized: Unauthorized: Authorization Error

Open pwalesdi opened this issue 6 years ago • 6 comments

I am trying to to create a list in my jupyter notebook for my activities data that I pulled but it keeps returning this error.

from stravalib.client import Client client = Client(access_token=access_token) activities = client.get_activities(limit=1000) sample = list(activities) sample.to_dict() this is the error I get: AccessUnauthorized: Unauthorized: Authorization Error: [{'resource': 'Athlete', 'field': 'access_token', 'code': 'invalid'}] I'm unsure why this isn't working.

pwalesdi avatar Jul 17 '19 18:07 pwalesdi

I get the same error -- code below.

athlete = client.get_athlete()
print("For {id}, I now have an access token {token}".format(id=athlete.id, token=access_token))

for activity in client.get_activities(after = "2010-01-01T00:00:00Z",  limit=5):
    print("{0.name} {0.moving_time}".format(activity))

As you can see below, I'm able to retrieve the Athlete, but I can't retrieve the activities.

No such attribute summit on entity <Athlete id=REMOVED firstname=REMOVED lastname=REMOVED>
For REMOVED I now have an access token REMOVED
Traceback (most recent call last):
  File ".\strava_collect.py", line 41, in <module>
    for activity in client.get_activities(after = "2010-01-01T00:00:00Z",  limit=5):
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\client.py", line 1666, in __next__
    return self.next()
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\client.py", line 1672, in next
    self._fill_buffer()
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\client.py", line 1643, in _fill_buffer
    raw_results = self.result_fetcher(page=self._page, per_page=self.per_page)
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\protocol.py", line 288, in get
    return self._request(url, params=params, check_for_errors=check_for_errors, use_webhook_server=use_webhook_server)
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\protocol.py", line 210, in _request
    self._handle_protocol_error(raw)
  File "C:\ProgramData\Anaconda3\lib\site-packages\stravalib\protocol.py", line 257, in _handle_protocol_error
    raise exc_class(msg, response=response)
stravalib.exc.AccessUnauthorized: Unauthorized: Authorization Error: [{'resource': 'AccessToken', 'field': 'activity:read_permission', 'code': 'missing'}]

krwester avatar Aug 06 '19 04:08 krwester

Hi guys,

Make sure to have set the scope specifically to match your needs. I need all (public + private) profile information and public detailed activity information, and have my scope set as below:

scope = ['read', 'profile:read_all', 'activity:read'] client.authorization_url(client_id=strava_client_id, redirect_uri=redirect_uri, scope=scope)

Valid values are 'read', 'read_all', 'profile:read_all', 'profile:write', 'profile:read_all', 'activity:read_all', 'activity:write'.

The Strava API doc shows what scope you need for each request: https://developers.strava.com/docs/reference/

Hope this helps, Pieter

35944 avatar Aug 06 '19 06:08 35944

That did it. Thank you!

krwester avatar Aug 07 '19 02:08 krwester

Hi, I'm facing the same error. My code is

from stravalib.client import Client

access_token = 'xxx'
refresh_token = 'xxx'
client_id = 00000
client = Client(access_token=access_token)
scope = ['read', 'profile:read_all', 'activity:read']
redirect_uri = 'http://127.0.0.1:5000/authorization'

client.authorization_url(
    client_id=client_id, redirect_uri=redirect_uri, scope=scope)


athlete = client.get_athlete() # Works fine
firstname = athlete.firstname
lastname = athlete.lastname

activities = client.get_activities(limit=1000)

for activity in activities:
    print(activity) # throws error

Can you provide any feedback on how to fix it?

samirak93 avatar Jun 22 '20 03:06 samirak93

@samirak93 If access_token is your default access token that you got when creating your app, its scope is always read. Did you use the authorization URL you created with client.authorization_url() to request a new token with the additional scopes? See e.g. https://developers.strava.com/docs/authentication/

jsamoocha avatar Jun 22 '20 05:06 jsamoocha

Got it to work. Apologies to have missed that part. Thanks.

samirak93 avatar Jun 23 '20 01:06 samirak93