Home > database >  FLASK, How to redirect Google autentication to a local html.file
FLASK, How to redirect Google autentication to a local html.file

Time:02-11

Hi everyone I have implemented the Google Authentication with API. I would like that once the user is authenticated the page redirect to a local html/Javascript application. I am trying the following code which is not working because it is not finding the correct url.

from flask import Flask, redirect, url_for, session, request, render_template from authlib.integrations.flask_client import OAuth import os from datetime import timedelta

# App config
app = Flask(__name__)
app.secret_key = "random secret"

# oAuth Setup
oauth = OAuth(app)
google = oauth.register(
    name='google',
    client_id='',
    client_secret='',
    access_token_url='https://accounts.google.com/o/oauth2/token',
    access_token_params=None,
    authorize_url='https://accounts.google.com/o/oauth2/auth',
    authorize_params=None,
    api_base_url='https://www.googleapis.com/oauth2/v1/',
    client_kwargs={'scope': 'openid email profile'},
)

@app.route('/')
def hello_world():
    email = dict(session).get('email',None)
    return f'Hello, {email}!'

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


@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')
    token = google.authorize_access_token()
    resp = google.get('userinfo')
    user_info = resp.json()
    session['email'] = user_info['email']
    return redirect('file:///home/luca/Desktop/ft_userdata/ProvaLogIn/prova.htm')





if __name__ == "__main__":
    app.run(debug=True)

Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

THe html that i am trying to render is simply an html local file with wrote hello

CodePudding user response:

you need to create folder called templates inside your project in this folder you will add your html file in your case it will be prova.html

and change the return statement to

    return render_template('prova.html')

this how should your directory tree should look like (general view)

├── app.py
├── static
└── templates
    └── status.html

hope this solve your issue

  • Related