-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_tokens.py
41 lines (35 loc) · 1.26 KB
/
generate_tokens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import json
import os.path
from os import path
import strava_sdk as ssdk
if (path.exists(ssdk.path_tokens)):
print("delete strava_tokens.json first")
quit()
with open(ssdk.path_credentials, 'r') as credentials:
credential = json.load(credentials)
for verifycredentials in ['client_id', 'client_secret', 'code']:
if (credential[verifycredentials]) is None:
print("Can't read", verifycredentials,"from credentials.json")
quit()
# Make Strava auth API call with your
# client_code, client_secret and code
response = requests.post(
url = 'https://www.strava.com/oauth/token',
data = {
'client_id': credential['client_id'],
'client_secret': credential['client_secret'],
'code': credential['code'],
'grant_type': 'authorization_code'
}
)
#Save json response as a variable
strava_tokens = response.json()
# Save tokens to file
with open(ssdk.path_tokens, 'w') as outfile:
json.dump(strava_tokens, outfile)
# Open JSON file and print the file contents
# to check it's worked properly
with open(ssdk.path_tokens) as check:
data = json.load(check)
print(data)