Hi Folks,
Trying to use the API to upload activities recorded by a Chinese Bicycle Computer, that junk product cannot automatically upload data to Strava, so I have to write a script to get those fit files via adb, and then upload them to strava via api.
However I got response like this:
Upload failed: {"message":"Authorization Error","errors":[{"resource":"AccessToken","field":"activity:write_permission","code":"missing"}]}
I used
scope = ['activity:write']
when getting an access_token, and I found out I always get the same access token whatever I set the scope to be.
That access token is also the one showed on https://www.strava.com/settings/api, under that I can see scope: read, just like https://communityhub.strava.com/t5/developer-discussions/authentication-scope/m-p/6357. In order to upload fit files I need a token with "activity:write", but I cannot find anywhere to change the scope of my access token.
Of course I tried to solved this by https://developers.strava.com/docs/authentication/ , however its not helping well.
here is my demo written by python
from requests_oauthlib import OAuth2Session
import requests
def get_access_token():
client_id = "client_id"
client_secret = "client_secret"
refresh_token = "refresh_token "
scope = ["activity:write"]
oauth = OAuth2Session(client_id=client_id, redirect_uri="http://localhost", scope=scope)
token_url = "https://www.strava.com/oauth/token"
extra = {
"client_id": client_id,
"client_secret": client_secret,
}
token = oauth.refresh_token(token_url, refresh_token=refresh_token, **extra)
print(token)
return token["access_token"]
access_token = get_access_token()
headers = {'Authorization': 'Bearer ' + access_token}
activity_type = 'ride' # or 'run', 'swim', etc.
name = 'activity20230503172414'
description = 'upload by api'
commute = False # whether this activity is a commute
trainer = False # whether this activity was done on a trainer
data_type = 'fit'
url = 'https://www.strava.com/api/v3/uploads'
fit_file = open('20230503172414.fit', 'rb')
files = {'file': ('activity.fit', fit_file, 'application/octet-stream')}
params = {
'data_type': data_type,
'activity_type': activity_type,
'name': name,
'description': description,
'commute': commute,
'trainer': trainer
}
response = requests.post(url, headers=headers, data=params, files=files)
if response.status_code == 200:
print('Upload successful')
else:
print('Upload failed:', response.text)