-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.py
44 lines (34 loc) · 1.44 KB
/
app.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
import os
from flask import Flask, jsonify
from flask import url_for
from dotenv import load_dotenv
load_dotenv()
import requests
from authlib.integrations.flask_client import OAuth
LICHESS_HOST = os.getenv("LICHESS_HOST", "https://lichess.org")
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")
app.config['LICHESS_CLIENT_ID'] = os.getenv("LICHESS_CLIENT_ID")
app.config['LICHESS_AUTHORIZE_URL'] = f"{LICHESS_HOST}/oauth"
app.config['LICHESS_ACCESS_TOKEN_URL'] = f"{LICHESS_HOST}/api/token"
oauth = OAuth(app)
oauth.register('lichess', client_kwargs={"code_challenge_method": "S256"})
@app.route('/')
def login():
redirect_uri = url_for("authorize", _external=True)
"""
If you need to append scopes to your requests, add the `scope=...` named argument
to the `.authorize_redirect()` method. For admissible values refer to https://lichess.org/api#section/Authentication.
Example with scopes for allowing the app to read the user's email address:
`return oauth.lichess.authorize_redirect(redirect_uri, scope="email:read")`
"""
return oauth.lichess.authorize_redirect(redirect_uri)
@app.route('/authorize')
def authorize():
token = oauth.lichess.authorize_access_token()
bearer = token['access_token']
headers = {'Authorization': f'Bearer {bearer}'}
response = requests.get(f"{LICHESS_HOST}/api/account", headers=headers)
return jsonify(**response.json())
if __name__ == '__main__':
app.run()