I'm trying to build a website with a Flask backend where I can upload videos to youtube. I'm already through the quota and able to upload videos through a script if I run it as a local webserver.
Now the problem is that I'm not sure what I need to use as a redirect URI when I want to deploy it in a Docker Container. I thought I can just set the URI in the Google console to https://vod.mydomain.dev/oauth2callback and adjust the oauth and change it in my code but I always get this error:
oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response.But I don't understand why. So my question is how can you Authorize google via oauth on a webpage deployed in a docker container?
import osimport google.oauth2.credentialsimport google_auth_oauthlib.flowfrom googleapiclient.errors import HttpErrorfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaFileUploadfrom flask import Flask, request, redirect, session, url_forapp = Flask(__name__)app.secret_key = os.urandom(24)# Ersetzen Sie YOUR_CLIENT_ID und YOUR_CLIENT_SECRET mit Ihren eigenen Wertenos.environ['GOOGLE_CLIENT_ID'] = os.environ['GOOGLE_CLIENT_SECRET'] = os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'CLIENT_SECRETS_FILE = "client_secret.json"SCOPES = ['https://www.googleapis.com/auth/youtube.upload']@app.route('/')def index(): return 'Hello World! <a href="/authorize">Authorize</a> '+ url_for('oauth2callback', _external=True, _scheme='https')@app.route('/authorize')def authorize(): flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( CLIENT_SECRETS_FILE, scopes=SCOPES) flow.redirect_uri = url_for('oauth2callback', _external=True, _scheme='https') authorization_url, state = flow.authorization_url( access_type='offline', include_granted_scopes='true') session['state'] = state print(session['state'],flow.redirect_uri) return redirect(authorization_url)@app.route('/oauth2callback')def oauth2callback(): state = session['state'] flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( CLIENT_SECRETS_FILE, scopes=SCOPES, state=state) flow.redirect_uri = url_for('oauth2callback', _external=True) authorization_response = request.url flow.fetch_token(authorization_response=authorization_response) credentials = flow.credentials session['credentials'] = {'token': credentials.token,'refresh_token': credentials.refresh_token,'token_uri': credentials.token_uri,'client_id': credentials.client_id,'client_secret': credentials.client_secret,'scopes': credentials.scopes } return redirect('/upload_video')@app.route('/upload_video')def upload_video(): if 'credentials' not in session: return redirect('authorize') credentials = google.oauth2.credentials.Credentials(**session['credentials']) try: youtube = build('youtube', 'v3', credentials=credentials) body = {"snippet": {"categoryId": "22","description": "Beschreibung des Videos","title": "Funktioniert der Automatische Upload?" },"status": {"privacyStatus": "private" } } video_file = "/clips/Test.mp4" media = MediaFileUpload(video_file, mimetype='video/mp4', chunksize=-1, resumable=True) request = youtube.videos().insert( part=",".join(body.keys()), body=body, media_body=media ) response = request.execute() return f'Video wurde erfolgreich hochgeladen: {response}' except HttpError as error: return f'Ein Fehler ist aufgetreten: {error}'if __name__ == '__main__': app.run(host="0.0.0.0", port=443)I tried to run the script directly and use a local webserver and everything worked fine.
Then I tried to create a docker container and I'm still able to get to the login screen but the moment it tries to redirect me back to my own domain I get an error and it crashes.