Forums

Cant launch LocalWebserverAuth in pythonanywhere

This code gauth.LocalWebserverAuth() isnt working in the webapp hosted in this website. In the localhost this piece of line opens a google authentication window where you can login in but fails to open in my hosted app.

 from pydrive.auth import GoogleAuth
 from pydrive.drive import GoogleDrive

 gauth = GoogleAuth()
 # Try to load saved client credentials
 gauth.LoadCredentialsFile("mycreds.txt")
 if gauth.credentials is None:
     # Authenticate if they're not there
     gauth.LocalWebserverAuth()
 elif gauth.access_token_expired:
     # Refresh them if expired
     gauth.Refresh()
 else:
     # Initialize the saved creds
     gauth.Authorize()
 # Save the current credentials to a file
 gauth.SaveCredentialsFile("mycreds.txt")

 drive = GoogleDrive(gauth)

This is the error I am getting

 home/eyal360/.virtualenvs/fbvenv/lib/python3.9/site- packages/oauth2client/_helpers.py:255: UserWarning: 
Cannot access mycreds.txt: No such file or directory
2022-04-26 03:34:52,114:

warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

Use absolute path to mycreds.txt, not relative one.

Relative path is working in other code blocks. But thats not the point. When for the first time a user uses this Google API, there wont be any mycreds.txt. So at that time, this library opens a window (oauth2 screen of google) where the user signs in and that instance a new file will be saved called mycreds.txt. When the user again uses the API, his/her credentials are fetched from this .txt file.

The above error comes when the user uses the API for the first time. The function gauth.LocalWebserverAuth(hostname, port number), by default they use host name as the localhost and port numbers are [8080, 8090]. I think I need to configure it here.

When I only use this code then no error comes in the error.logs but no authentication window is opening of Google OAuth2.

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

Running a local web server for authentication will not work on PythonAnywhere. You will need to use a different authentication method.

Can suggest a way, where I can do that?

You should check the Google docs, if there are alternative ways mentioned.