-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (51 loc) · 1.67 KB
/
app.py
File metadata and controls
66 lines (51 loc) · 1.67 KB
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
64
65
66
import os
import json
import urllib.parse
from dotenv import load_dotenv
from flask import Flask, redirect, request
import requests
load_dotenv()
app = Flask(__name__)
CLIENT_ID = os.environ["GOOGLE_CLIENT_ID"]
CLIENT_SECRET = os.environ["GOOGLE_CLIENT_SECRET"]
SCOPE = os.environ["GOOGLE_SCOPE"]
ACCESS_TYPE = os.environ["GOOGLE_ACCESS_TYPE"]
REDIRECT_URI = "http://localhost:8080/callback"
GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth"
GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token"
@app.route("/")
def index():
params = {
"client_id": CLIENT_ID,
"redirect_uri": REDIRECT_URI,
"response_type": "code",
"scope": SCOPE.replace(",", " "),
"access_type": ACCESS_TYPE,
"prompt": "consent",
}
auth_url = f"{GOOGLE_AUTH_URL}?{urllib.parse.urlencode(params)}"
return redirect(auth_url)
@app.route("/callback")
def callback():
code = request.args.get("code")
if not code:
error = request.args.get("error", "unknown error")
return f"<h1>Error</h1><pre>{error}</pre>", 400
token_response = requests.post(
GOOGLE_TOKEN_URL,
data={
"code": code,
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"redirect_uri": REDIRECT_URI,
"grant_type": "authorization_code",
},
)
token_data = token_response.json()
formatted = json.dumps(token_data, indent=2)
return f"""
<h1>Google OAuth Response</h1>
<pre style="background:#f4f4f4;padding:16px;border-radius:8px;font-size:14px;">{formatted}</pre>
"""
if __name__ == "__main__":
app.run(host="localhost", port=8080, debug=True)