iterative / PyDrive2

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

Authenticate in a hosted website #170

Closed meet1919 closed 2 years ago

meet1919 commented 2 years ago

I have a website where I want to use PyDrive2 for automating the Google Drive operations.

gauth = GoogleAuth()

''' Try to load saved client credentials -------- START '''
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")
''' ----------------------------------------------- END ''' 

gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth) 

The above code works fine in the localhost, but this function gauth.LocalWebserverAuth() doesn't opens an authentication window in the hosted website. Do I need to change the args of LocalWebserverAuth(host, port_number)?

shcheklein commented 2 years ago

@meet1919 hi!

I think you don't need all that logic with LoadCredentials, Refresh, etc. It should be handled by the PyDrive2 already (unless you have some unique situation that I'm not aware of). You should place the settings.yaml file nearby as described here:

Here is the sample:

https://docs.iterative.ai/PyDrive2/oauth/#sample-settings-yaml

and the most important part:

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

replaces the need to save/load things manually.

but this function gauth.LocalWebserverAuth() doesn't opens an authentication window in the hosted website.

could you describe what kind of behavior do expect? Do you mean something like this https://docs.iterative.ai/PyDrive2/oauth/#building-your-own-authentication-flow ?

meet1919 commented 2 years ago

I have a website hosted on PythonAnywhere. As described earlier LocalWebserverAuth() fails to open Screenshot (880) this kind of authentication page. Can I make a custom one with the GetAuthUrl()

On the docs I saw this sample code:

from pydrive2.auth import GoogleAuth

gauth = GoogleAuth()
auth_url = gauth.GetAuthUrl() # Create authentication url user needs to visit
code = AskUserToVisitLinkAndGiveCode(auth_url) # Your customized authentication flow
gauth.Auth(code) # Authorize and build service from the code

I dont understand the last second line AskUserToVisitLinkAndGiveCode(auth_url). What does this funciton do? Also I am using Django for the backend. Please suggest how to create a custom oauth2 window like the above photo auto generate by LocalWebServerAuth().

shcheklein commented 2 years ago

To be honest, I'm not sure that it 100% possible. But idea here should be similar to what is described here:

https://developers.google.com/identity/protocols/oauth2/web-server#python_1

When you get that auth URL you need to ask your user to go there, e.g:

flask.redirect(authorization_url)

But also prepare your server to listen on the URL port that is provided via redirect_uri in the settings.yaml. It can be your server name + /oauth2callback . It will get a code that then needs to be provided to the Auth(code).

Again, not something I tried, and clearly this is a bit more involved since it requires server-client back-and-forth + handing responses and maybe even preserving some state in between.

Btw, do you expect multiple users giving you permissions to their Google Drive?

meet1919 commented 2 years ago

No, there is just a one user who wants to automate uploading to his own google drive. I understand the concept of building custom authentication flow but still not sure as how generate code with my existing website made on django

shcheklein commented 2 years ago

If it's a single user, can you use service account or authenticate that user in advance and save the credentials to the machine that is running the service?