iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
581 stars 69 forks source link

Running PyDrive on an EC2 instance with Docker #166

Closed TomasVCMU closed 2 years ago

TomasVCMU commented 2 years ago

I am struggling to get past an Authorization exception when running PyDrive to upload a CSV file for an inventory website I am hosting.

My code lives on an EC2 instance and runs with Docker. See https://github.com/AashaiAvadhani1/FLP-Inventory for our codebase. In the path inventory/views.py (https://github.com/AashaiAvadhani1/FLP-Inventory/blob/master/inventory/views.py) I have a function that does the authorization google_auth(). I get the AuthorizationException when running gauth.LocalWebserverAuth()

In the docker-compose.yml file, I have allowed ports 8080 and 8090 (Current file isn't updated, but an image is provided here of my file). Note: my settings.yaml file is not in this github for security purposes, but my program runs fine when I run the code locally - so it shouldn't be an issue with that. I have also updated my security group with inbound rules allowing these ports to no avail. Any help would be much appreciated. Screen Shot 2022-04-21 at 7 23 56 PM Screen Shot 2022-04-21 at 7 24 46 PM

I have also tried changing the host_name field in the LocalWebserverAuth() to "flpinventory.com" since that is my website. This also does not work. Does PyDrive2, in its current implementation, simply not allow this to work on live websites? I can't find any documentation or examples online, either. Thanks.

shcheklein commented 2 years ago

@TomasVCMU I would first check the settings for the client that you are using:

Screen Shot 2022-04-21 at 6 54 01 PM

specifically, in the Authorized redirect URIs you should provide address of the server, including its port (8080 or 8090 - not sure). Something like: https://flpinventory.com:8080.

Then you need to provide the same redirect_uri in the settings.yaml file (see this).

If this doesn't work please share the exception message / stack trace that you are getting?

TomasVCMU commented 2 years ago

Thanks! Can you expand on what I would change the redirect_uri to?

TomasVCMU commented 2 years ago

@shcheklein I tried this. Doesn't seem to be working still. Here are images of what I added as you instructed as well as my docker logs output.

Screen Shot 2022-04-21 at 10 27 19 PM Screen Shot 2022-04-21 at 10 27 43 PM Screen Shot 2022-04-21 at 10 28 24 PM

shcheklein commented 2 years ago

@TomasVCMU one of the same https://flpinventory.com:8080 or https://flpinventory.com:8090

shcheklein commented 2 years ago

Btw - are using PyDrive or PyDrive2? PyDrive is not maintained anymore and can have some old unresolved problems.

shcheklein commented 2 years ago

And please, don't pass anything to LocalWebserverAuth(), you don't need it.

TomasVCMU commented 2 years ago

@shcheklein Oops, just noticed I was running an old version with pyDrive instead of pyDrive2. However, I just changed it and I also removed the host_name passed to LocalWebserverAuth(). Still no luck. Same Auth exception is raised.

Does the redirect_uri need to be path specific. The call is being made on flpinventory.com/report/ . Should I be making my redirect_uri on the Google Console and settings.yaml to be flpinventory.com:8080/report/ or is flpinventory.com:8080 fine? I'm not sure what could be going wrong here. Ports allowed on docker, ec2, and now redirect uri is fixed on Google Console + settings.yaml

Logs below: Screen Shot 2022-04-22 at 9 33 39 AM

shcheklein commented 2 years ago

Does the redirect_uri need to be path specific.

It should be exact URL that can be used to access that 8080 port in the container.

This error that you are getting means this:

                "Failed to start a local web server. Please check your firewall"
            )
            print(
                "settings and locally running programs that may be blocking or"
            )
            print("using configured ports. Default ports are 8080 and 8090.")

It might be that something is already running that takes 8080 and 8090? Another instance of this app? Review the way you map ports?

TomasVCMU commented 2 years ago

@shcheklein I can try to change the port to some arbitrary number, but I doubt we're using those ports at all considering I only recently allowed them in my EC2 admin panel. So, if it was the case that those ports were being used, then those wouldn't have gone through either way. The only mapping I'm using is for port 80 and port 443 using Caddy as a reverse proxy for Certbot for SSL certificates.

Should I then change the redirect_uri to be flpinventory.com:8080/report/ to make up for this?

shcheklein commented 2 years ago

First, I still see that it can't start the local HTTP server inside the container for whatever reason (logs are not good, PyDrive2 should be fixed to throw something more meaningful). What I would is to SSH into container and would try to run a simple HTTP server from the Python console on the same port - 8080, 8090:

import http.server
httpd = http.server.HTTPServer(('', 8080), None)
httpd.serve_forever()

You should be then able to access flpinventory.com:8080 (or it might depend on your setup you need to specify the report/)

Should I then change the redirect_uri to be flpinventory.com:8080/report/ to make up for this?

I doubt. When you run flpinventory.com does it access the same container?