I'm trying to create a Flask app that uses a Google login and then pulls some analytics about the user's youtube channel. I'm still learning Python & Flask
I've been able to create this locally in Python using a method which uses a saved .json file and InstalledAppFlow, but I'm struggling to deploy this to Flask. I test on a local host, but deploy to a website
Below is what I have so far (some parts scrubbed for privacy). I've tried returning various things to the session variable 'user_credentails', looked at many different online tutorials, and read the documentation here (https://docs.authlib.org/en/latest/client/requests.html) but haven't been able to figure it out. I've tried to play around with the "google.get('userinfo') piece too, but it isn't working.
I get the error "AttributeError: 'dict' object has no attribute 'authorize'" with the code below, at this part "youtube = build('youtube','v3',credentials=user_info_local)" I cant figure out what to return that has the 'authorize' attribute required.
How exactly can I get authorization credentials returned and passed to the 'pull_videos' function?
from flask import Flask, redirect, url_for, sessionfrom authlib.integrations.flask_client import OAuthfrom google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from googleapiclient.discovery import build app = Flask(__name__)app.secret_key = 'PRIVATE' #Hiding this since putting it online#Oauth configoauth = OAuth(app)google = oauth.register( name='google', client_id='PRIVATE', #Hiding this since putting it online client_secret='PRIVATE', #Hiding this since putting it online 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 https://www.googleapis.com/auth/youtube.readonly youtube'}, )@app.route("/")def index(): 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(): ###I've tried tweaking many things here but haven't been able to get it to work google = oauth.create_client('google') token = google.authorize_access_token() resp = google.get('userinfo') user_info = resp.json() session['user_credentials']=user_info # do something with the token and profile session['email'] = user_info['email'] return redirect('/')@app.route('/pull_videos')def pull_videos(): query_page_token = 0 user_info_local = session.get('user_credentials', None) youtube = build('youtube','v3',credentials=user_info_local) return ...if __name__ == "__main__": app.run(host="127.0.0.1", port=8080, debug=True)
See above, I included what I tried and was expecting