Spaces:
Runtime error
Runtime error
create flask_app.py
Browse files- flask_app.py +68 -0
flask_app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, redirect, request, session, jsonify
|
| 2 |
+
from google_auth_oauthlib.flow import Flow
|
| 3 |
+
from google.oauth2 import id_token
|
| 4 |
+
from google.auth.transport import requests
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
app.secret_key = 'YOUR_FLASK_SECRET_KEY'
|
| 9 |
+
|
| 10 |
+
# Google OAuth Configuration
|
| 11 |
+
CLIENT_ID = '931024815427-72meiq3uivuuolfukb3jvikvhhqlr574.apps.googleusercontent.com'
|
| 12 |
+
CLIENT_SECRET = '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'
|
| 13 |
+
REDIRECT_URI = 'http://localhost:5001/oauth2callback'
|
| 14 |
+
SCOPES = ['openid', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']
|
| 15 |
+
|
| 16 |
+
@app.route('/login')
|
| 17 |
+
def login():
|
| 18 |
+
flow = Flow.from_client_config(
|
| 19 |
+
client_config={
|
| 20 |
+
"web": {
|
| 21 |
+
"client_id": CLIENT_ID,
|
| 22 |
+
"client_secret": CLIENT_SECRET,
|
| 23 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 24 |
+
"token_uri": "https://oauth2.googleapis.com/token"
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
scopes=SCOPES,
|
| 28 |
+
redirect_uri=REDIRECT_URI
|
| 29 |
+
)
|
| 30 |
+
authorization_url, state = flow.authorization_url(access_type='offline', include_granted_scopes='true')
|
| 31 |
+
session['state'] = state
|
| 32 |
+
return redirect(authorization_url)
|
| 33 |
+
|
| 34 |
+
@app.route('/oauth2callback')
|
| 35 |
+
def oauth2callback():
|
| 36 |
+
state = session['state']
|
| 37 |
+
flow = Flow.from_client_config(
|
| 38 |
+
client_config={
|
| 39 |
+
"web": {
|
| 40 |
+
"client_id": CLIENT_ID,
|
| 41 |
+
"client_secret": CLIENT_SECRET,
|
| 42 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 43 |
+
"token_uri": "https://oauth2.googleapis.com/token"
|
| 44 |
+
}
|
| 45 |
+
},
|
| 46 |
+
scopes=SCOPES,
|
| 47 |
+
state=state,
|
| 48 |
+
redirect_uri=REDIRECT_URI
|
| 49 |
+
)
|
| 50 |
+
flow.fetch_token(authorization_response=request.url)
|
| 51 |
+
credentials = flow.credentials
|
| 52 |
+
session['credentials'] = credentials.to_json()
|
| 53 |
+
|
| 54 |
+
idinfo = id_token.verify_oauth2_token(
|
| 55 |
+
id_token=credentials.id_token,
|
| 56 |
+
request=requests.Request(),
|
| 57 |
+
audience=CLIENT_ID
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
session['user_info'] = {"name": idinfo.get("name")}
|
| 61 |
+
return redirect("http://localhost:7860") # Redirect back to the Shiny app
|
| 62 |
+
|
| 63 |
+
@app.route('/user_info')
|
| 64 |
+
def user_info():
|
| 65 |
+
return jsonify(session.get('user_info', {}))
|
| 66 |
+
|
| 67 |
+
if __name__ == '__main__':
|
| 68 |
+
app.run(port=5001)
|