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

# decorator for routes that should be accessible only by logged in users
# from auth_decorator import login_required

# dotenv setup
# from dotenv import load_dotenv
# load_dotenv()

GOOGLE_CLIENT_ID = "878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="36Kq2sfmUVms9ad5kp_rnQaU" 

# App config
app = Flask(__name__)
# Session config
app.secret_key = os.getenv("APP_SECRET_KEY")
app.config['SESSION_COOKIE_NAME'] = 'google-login-session'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)

# oAuth Setup
oauth = OAuth(app)
google = oauth.register(
    name='google',
    client_id=os.getenv("878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"),
    client_secret=os.getenv("36Kq2sfmUVms9ad5kp_rnQaU"),
    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/',
    userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',  # This is only needed if using openId to fetch user info
    client_kwargs={'scope': 'openid email profile'},
)


@app.route('/')
# @login_required
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {email}!'


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


@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')  # create the google oauth client
    token = google.authorize_access_token()  # Access token from google (needed to get user info)
    resp = google.get('userinfo')  # userinfo contains stuff u specificed in the scrope
    user_info = resp.json()
    user = oauth.google.userinfo()  # uses openid endpoint to fetch user info
    # Here you use the profile/user data that you got and query your database find/register the user
    # and set ur own data in the session not the profile from google
    session['profile'] = user_info
    session.permanent = True  # make the session permanant so it keeps existing after broweser gets closed
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')
# from flask import Flask, render_template, session, request, redirect, abort, jsonify
# import requests

# app = Flask(__name__)

# GOOGLE_CLIENT_ID = "878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"
# GOOGLE_CLIENT_SECRET="36Kq2sfmUVms9ad5kp_rnQaU" 


# @app.route("/login")
# def login():
#     if request.args.get("next"):
#         session["next"] = request.args.get("next")
#     return redirect(f"https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/userinfo.profile&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://note-web.herokuapp.com/authorized&client_id={GOOGLE_CLIENT_ID}")


# @app.route("/authorized")
# def google_authorized():
#     r = requests.post("https://oauth2.googleapis.com/token", data={
#         "client_id": GOOGLE_CLIENT_ID,
#         "client_secret": GOOGLE_CLIENT_SECRET,
#         "code": request.args.get("code"),
#         "grant_type": "authorization_code",
#         "redirect_uri": "https://note-web.herokuapp.com/authorized"
#     })
#     print(r.json())
#     return r.json()
#     # print(colored(r.json(), "red"))
#     # r = requests.get(f'https://www.googleapis.com/oauth2/v2/userinfo?access_token={r.json()["access_token"]}').json()
#     # user = c.execute("SELECT * FROM users WHERE user_id=:user_id", {"user_id": r["id"]}).fetchall()
#     # if len(user) != 0:
#     #     session["user_id"] = user[0][0]
#     #     session["name"] = user[0][1]
#     #     session["avatar"] = user[0][2]
#     # else:
#     #     c.execute("INSERT INTO users (user_id, name, photo) VALUES (:id, :name, :photo)", {"id": r["id"], "name": r["name"], "photo": r["picture"]})
#     #     conn.commit()
#     #     session["user_id"] = r["id"]
#     #     session["name"] = r["name"]
#     #     session["avatar"] = r["picture"]

#     # if session.get("next"):
#     #     return redirect(session.get("next"))
#     # return redirect("/")

if __name__ == '__main__':
    app.run(debug=True, port=5000)


3dae1dc5ac6f7784762c78c51a9cc89fef58013e
<mask token>


@app.route('/')
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {email}!'


<mask token>


@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')
    token = google.authorize_access_token()
    resp = google.get('userinfo')
    user_info = resp.json()
    user = oauth.google.userinfo()
    session['profile'] = user_info
    session.permanent = True
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')


<mask token>

<mask token>


@app.route('/')
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {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()
    user = oauth.google.userinfo()
    session['profile'] = user_info
    session.permanent = True
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')


if __name__ == '__main__':
    app.run(debug=True, port=5000)

<mask token>
GOOGLE_CLIENT_ID = (
    '878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com')
GOOGLE_CLIENT_SECRET = '36Kq2sfmUVms9ad5kp_rnQaU'
app = Flask(__name__)
app.secret_key = os.getenv('APP_SECRET_KEY')
app.config['SESSION_COOKIE_NAME'] = 'google-login-session'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)
oauth = OAuth(app)
google = oauth.register(name='google', client_id=os.getenv(
    '878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com'
    ), client_secret=os.getenv('36Kq2sfmUVms9ad5kp_rnQaU'),
    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/', userinfo_endpoint
    ='https://openidconnect.googleapis.com/v1/userinfo', client_kwargs={
    'scope': 'openid email profile'})


@app.route('/')
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {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()
    user = oauth.google.userinfo()
    session['profile'] = user_info
    session.permanent = True
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')


if __name__ == '__main__':
    app.run(debug=True, port=5000)

from flask import Flask, redirect, url_for, session
from authlib.integrations.flask_client import OAuth
import os
from datetime import timedelta
GOOGLE_CLIENT_ID = (
    '878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com')
GOOGLE_CLIENT_SECRET = '36Kq2sfmUVms9ad5kp_rnQaU'
app = Flask(__name__)
app.secret_key = os.getenv('APP_SECRET_KEY')
app.config['SESSION_COOKIE_NAME'] = 'google-login-session'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)
oauth = OAuth(app)
google = oauth.register(name='google', client_id=os.getenv(
    '878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com'
    ), client_secret=os.getenv('36Kq2sfmUVms9ad5kp_rnQaU'),
    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/', userinfo_endpoint
    ='https://openidconnect.googleapis.com/v1/userinfo', client_kwargs={
    'scope': 'openid email profile'})


@app.route('/')
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {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()
    user = oauth.google.userinfo()
    session['profile'] = user_info
    session.permanent = True
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')


if __name__ == '__main__':
    app.run(debug=True, port=5000)

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

# decorator for routes that should be accessible only by logged in users
# from auth_decorator import login_required

# dotenv setup
# from dotenv import load_dotenv
# load_dotenv()

GOOGLE_CLIENT_ID = "878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="36Kq2sfmUVms9ad5kp_rnQaU" 

# App config
app = Flask(__name__)
# Session config
app.secret_key = os.getenv("APP_SECRET_KEY")
app.config['SESSION_COOKIE_NAME'] = 'google-login-session'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)

# oAuth Setup
oauth = OAuth(app)
google = oauth.register(
    name='google',
    client_id=os.getenv("878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"),
    client_secret=os.getenv("36Kq2sfmUVms9ad5kp_rnQaU"),
    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/',
    userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',  # This is only needed if using openId to fetch user info
    client_kwargs={'scope': 'openid email profile'},
)


@app.route('/')
# @login_required
def hello_world():
    email = dict(session)['profile']['email']
    return f'Hello, you are logge in as {email}!'


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


@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')  # create the google oauth client
    token = google.authorize_access_token()  # Access token from google (needed to get user info)
    resp = google.get('userinfo')  # userinfo contains stuff u specificed in the scrope
    user_info = resp.json()
    user = oauth.google.userinfo()  # uses openid endpoint to fetch user info
    # Here you use the profile/user data that you got and query your database find/register the user
    # and set ur own data in the session not the profile from google
    session['profile'] = user_info
    session.permanent = True  # make the session permanant so it keeps existing after broweser gets closed
    return redirect('/')


@app.route('/logout')
def logout():
    for key in list(session.keys()):
        session.pop(key)
    return redirect('/')
# from flask import Flask, render_template, session, request, redirect, abort, jsonify
# import requests

# app = Flask(__name__)

# GOOGLE_CLIENT_ID = "878650034451-gn5g4nm1j4of2at7pui7e28fa9pm3nc3.apps.googleusercontent.com"
# GOOGLE_CLIENT_SECRET="36Kq2sfmUVms9ad5kp_rnQaU" 


# @app.route("/login")
# def login():
#     if request.args.get("next"):
#         session["next"] = request.args.get("next")
#     return redirect(f"https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/userinfo.profile&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://note-web.herokuapp.com/authorized&client_id={GOOGLE_CLIENT_ID}")


# @app.route("/authorized")
# def google_authorized():
#     r = requests.post("https://oauth2.googleapis.com/token", data={
#         "client_id": GOOGLE_CLIENT_ID,
#         "client_secret": GOOGLE_CLIENT_SECRET,
#         "code": request.args.get("code"),
#         "grant_type": "authorization_code",
#         "redirect_uri": "https://note-web.herokuapp.com/authorized"
#     })
#     print(r.json())
#     return r.json()
#     # print(colored(r.json(), "red"))
#     # r = requests.get(f'https://www.googleapis.com/oauth2/v2/userinfo?access_token={r.json()["access_token"]}').json()
#     # user = c.execute("SELECT * FROM users WHERE user_id=:user_id", {"user_id": r["id"]}).fetchall()
#     # if len(user) != 0:
#     #     session["user_id"] = user[0][0]
#     #     session["name"] = user[0][1]
#     #     session["avatar"] = user[0][2]
#     # else:
#     #     c.execute("INSERT INTO users (user_id, name, photo) VALUES (:id, :name, :photo)", {"id": r["id"], "name": r["name"], "photo": r["picture"]})
#     #     conn.commit()
#     #     session["user_id"] = r["id"]
#     #     session["name"] = r["name"]
#     #     session["avatar"] = r["picture"]

#     # if session.get("next"):
#     #     return redirect(session.get("next"))
#     # return redirect("/")

if __name__ == '__main__':
    app.run(debug=True, port=5000)

