First step is to get an oauth token from strava.
Create a strava application
To do that, navigate to this link, changing the client id to match your app and authorize the strava test app. After you have clicked authorize, you will be redirected to a page that does not exist, but you can get the code from the URL :
http://localhost/token_exchange.php?state=&code=XXXX
Strava API docs : http://strava.github.io/api/v3/oauth/
strava_oauth_code = "7ba92a5340010f4035b2f897a7c93d6a9a331b53"
import requests
payload = {
'client_id':"5966",
'client_secret':"b8869c83423df058bbd72319cef18bd46123b251",
'code':strava_oauth_code
}
resp = requests.post("https://www.strava.com/oauth/token", params=payload)
assert resp.status_code == 200
access_token = resp.json()['access_token']
headers = {
'Authorization': "Bearer " + access_token
}
access_token
u'cc984d50e8c49b8cfd251d20ca9321d81c396d9d'
resp = requests.get("https://www.strava.com/api/v3/athlete", headers=headers)
assert resp.status_code == 200
athlete = resp.json()
print athlete['firstname'], athlete['lastname']
Julien Rebetez
def get_activities(page):
params = {
'per_page': 50,
'page':page
}
resp = requests.get("https://www.strava.com/api/v3/athlete/activities",
params=params, headers=headers)
assert resp.status_code == 200
activities = resp.json()
return activities
def get_all_activities():
all_activities = []
page = 1
while True:
activities = get_activities(page)
page += 1
if len(activities) == 0:
break
all_activities += activities
return all_activities
activities = get_all_activities()
print len(activities), ' activities total'
107 activities total
import json
with open('activities.json', 'w') as f:
json.dump(activities, f)