-
Notifications
You must be signed in to change notification settings - Fork 40
/
web_flask.py
executable file
·63 lines (50 loc) · 1.67 KB
/
web_flask.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
61
62
63
#!/usr/bin/env python3
# Install dependencies with:
# pip install flask requests requests_oauth2
import os
import logging
import requests
from requests_oauth2.services import GoogleClient
from requests_oauth2 import OAuth2BearerToken
from flask import Flask, request, redirect, session
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY") or os.urandom(20)
google_auth = GoogleClient(
client_id=("554229061086-np1qvffgq6gi1f6njg99qkeqt4h2gaut"
".apps.googleusercontent.com"),
client_secret="XqTsoS6DXq-W0KgTqvQISBOM",
redirect_uri="http://localhost:5000/google/oauth2callback",
)
@app.route("/")
def index():
return redirect("/google/")
@app.route("/google/")
def google_index():
if not session.get("access_token"):
return redirect("/google/oauth2callback")
with requests.Session() as s:
s.auth = OAuth2BearerToken(session["access_token"])
r = s.get("https://www.googleapis.com/plus/v1/people/me")
r.raise_for_status()
data = r.json()
return "Hello, {}!".format(data["displayName"])
@app.route("/google/oauth2callback")
def google_oauth2callback():
code = request.args.get("code")
error = request.args.get("error")
if error:
return "error :( {!r}".format(error)
if not code:
return redirect(google_auth.authorize_url(
scope=["profile", "email"],
response_type="code",
))
data = google_auth.get_token(
code=code,
grant_type="authorization_code",
)
session["access_token"] = data.get("access_token")
return redirect("/")
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
app.run(debug=True)