File size: 1,535 Bytes
769f727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ab8708
 
769f727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ab8708
769f727
 
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
# app.py

from shiny import App, render, ui
from shiny.flask import FlaskApp
from flask import Flask, redirect, url_for, session, request
from authlib.integrations.flask_client import OAuth

app = Flask(__name__)
app.secret_key = 'your_secret_key'

oauth = OAuth(app)
google = oauth.register(
    name='google',
    client_id='your_client_id',
    client_secret='your_client_secret',
    authorize_url='https://accounts.google.com/o/oauth2/auth',
    authorize_params=None,
    authorize_params={'scope': 'openid email profile'},
    access_token_url='https://accounts.google.com/o/oauth2/token',
    access_token_params=None,
    refresh_token_url=None,
    client_kwargs={'scope': 'openid email profile'},
)

@app.route('/login')
def login():
    redirect_uri = url_for('authorize', _external=True)
    return google.authorize_redirect(redirect_uri)

@app.route('/authorize')
def authorize():
    token = google.authorize_access_token()
    user = google.parse_id_token(token)
    session['user'] = user
    return redirect(url_for('index'))

@app.route('/logout')
def logout():
    del session['user']
    return redirect(url_for('index'))

@app.route('/')
def index():
    user = session.get('user')
    if user:
        username = user['name']
    else:
        username = "Guest"
    return f'Hello, {username}!'

# Embed Flask app within shiny-python
shiny_app = App(
    ui.page(ui.title("Google OAuth Example")),
    render.FlexDashboard("flex_dashboard", title="Google OAuth Example"),
    FlaskApp(app)
)

shiny_app.serve()