-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.py
60 lines (47 loc) · 1.57 KB
/
api.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import responder
import requests
import os
import urllib
from loguru import logger
api = responder.API()
def authorize_url():
"""Generate authorization uri"""
app_url = os.getenv('APP_URL', 'http://localhost')
logger.debug(f"APP_URL={app_url}")
params = {
"client_id": os.getenv('STRAVA_CLIENT_ID'),
"response_type": "code",
"redirect_uri": f"{app_url}:5042/authorization_successful",
"scope": "read,profile:read_all,activity:read",
"state": 'https://github.com/sladkovm/strava-oauth',
"approval_prompt": "force"
}
values_url = urllib.parse.urlencode(params)
base_url = 'https://www.strava.com/oauth/authorize'
rv = base_url + '?' + values_url
logger.debug(rv)
return rv
@api.route("/")
def home(req, resp):
resp.text = "Welcome to strava-oauth"
@api.route("/client")
def client(req, resp):
resp.text = os.getenv('STRAVA_CLIENT_ID')
@api.route("/authorize")
def authorize(req, resp):
"""Redirect user to the Strava Authorization page"""
api.redirect(resp, location=authorize_url())
@api.route("/authorization_successful")
def authorization_successful(req, resp):
"""Exchange code for a user token"""
params = {
"client_id": os.getenv('STRAVA_CLIENT_ID'),
"client_secret": os.getenv('STRAVA_CLIENT_SECRET'),
"code": req.params.get('code'),
"grant_type": "authorization_code"
}
r = requests.post("https://www.strava.com/oauth/token", params)
logger.debug(r.text)
resp.text = r.text
if __name__ == "__main__":
api.run(address="0.0.0.0")