Closed meet1919 closed 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 ?
I have a website hosted on PythonAnywhere. As described earlier LocalWebserverAuth()
fails to open
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()
.
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?
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
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?
I have a website where I want to use PyDrive2 for automating the Google Drive operations.
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 ofLocalWebserverAuth(host, port_number)
?