-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.py
executable file
·61 lines (48 loc) · 1.8 KB
/
oauth.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
import authlib
from authlib.client import OAuth2Session
import flask
app = flask.Flask(__name__)
app.secret_key = "idk what this is"
ACCESS_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'
AUTHORIZATION_URL = 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent'
try:
f=open("./util/key.txt","r")
except FileNotFoundError as e:
raise Exception('Error: key.txt file not found')
k=f.read().rstrip("\n")
f.close()
try:
f2=open("./util/secret.txt","r")
except FileNotFoundError as e:
raise Exception('Error: secret.txt file not found')
s=f2.read().rstrip("\n")
f2.close()
@app.route("/")
def home():
session = OAuth2Session(k, s,scope="profile email",redirect_uri='http://localhost:5000/second')
uri,state=session.authorization_url(AUTHORIZATION_URL)
flask.session.permanent=True
flask.session["auth_state"]=state
print(flask.session)
return flask.redirect(uri,code=302)
@app.route("/second")
def redirect():
state=flask.request.args.get("state",default=None,type=None)
print(flask.session)
# if state != flask.session["auth_state"]:
# return flask.render_template("oauth.html",something="Uhh you done messed up")
session = OAuth2Session(k, s,redirect_uri='http://localhost:5000/second')
oauth2_tokens = session.fetch_access_token(ACCESS_TOKEN_URI,authorization_response=flask.request.url)
print("OAUTH TOKEN: ")
print(oauth2_tokens)
flask.session["auth_token"]=oauth2_tokens
h=session.get("https://people.googleapis.com/v1/people/me?personFields=emailAddresses")
print(h)
try:
email=h.json()["emailAddresses"][0]["value"]
return flask.render_template("oauth.html",something=email)
except:
return flask.redirect("/")
if __name__ == "__main__":
app.debug = True
app.run()