I'm working on a simple web application to list google drive information for a currently logged in user using Flask session and Google OAuth. I'm running a DO droplet with Nginx and WSGI to serve the application. When I test the application locally, it works as expected and I'm able to log into the application using google Oauth. When I upload the app to the droplet though, I seem to be losing the session after logging in and getting redirected. The way the app is currently written, it keeps putting me at the login page even after logging in successfully. I'm not quite sure what I'm missing though or why its only happening on the server and not on my local machine.
Application:
from flask import Flask, redirect,request, session, abort, render_template
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import secrets
app = Flask(__name__)
secret_key = secrets.token_hex(16)
app.config['SECRET_KEY'] = secret_key
@app.route('/')
def index():
if not session.get('logged_in'):
return render_template('login.html')
drive = getDrive()
return render_template('index.html')
@app.route('/login', methods = ['POST'])
def login():
gauth = GoogleAuth()
auth_url = gauth.GetAuthUrl()
session['logged_in'] = True
return redirect(auth_url, code=302)
def getDrive(drive=None, gauth=None):
if not drive:
if not gauth:
gauth = GoogleAuth()
gauth.LoadCredentialsFile()
if gauth.access_token_expired:
try:
gauth.Refresh()
except Exception as e:
print(f'Google Drive Error: {e}')
else:
gauth.Authorize()
return GoogleDrive(gauth)
if drive.auth.access_token_expired:
try:
drive.auth.Refresh()
except Exception as e:
print(f'Google Drive Error: {e}')
return drive
Settings.yaml for Google Oauth:
client_config_backend: file
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json