Closed mislavperi closed 2 years ago
I have the same issue, not sure how to make it save the credentials and not ask again...
Without the settings.yaml
it ask for verification (code run fine) but with the settings.yaml
it does not proceed to the rest of the code (see below to reproduce).
settings.yaml
:
client_config_backend: settings
client_config:
client_id: id
client_secret: secret
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.install
- https://www.googleapis.com/auth/drive.metadata
main.py
:
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
drive = GoogleDrive(gauth)
fileList = drive.ListFile({'q': "sharedWithMe", 'orderBy': 'sharedWithMeDate desc', 'maxResults': 5}).GetList()
for file in fileList:
print('Title: %s, Shared on: %s, ID: %s' % (file['title'], file['sharedWithMeDate'], file['id']), )
@Alex-Nalin Honestly I'd just avoid PyDrive2 and switch to something like S3 or Digital Ocean spaces. I ended up fixing it by pasting the creds file from the older project and using that...
@mislavpericaxilis :
I think client_secret.json
is not needed if settings
is set as client_config_backend
and:
client_config:
client_id: id
client_secret: secret
are set.
Current use of the library.
Could you give more details? Where and how do you run it?
It might something setup specific. E.g. celery runs it in a temp directory each time or something.
Could you create a toy example that does auth and then runs a few requests in a row?
@mislavpericaxilis
I'd just avoid PyDrive2 and switch to something like S3 or Digital Ocean spaces.
I agree if it's possible and you don't need Google Drive functionality (e.g. UI that is accessible by less technical people). S3 is way easier to use programmatically. It requires some setup though which can be non-trivial compared to GDrive and eventually payments.
@mislavpericaxilis @Alex-Nalin I'm not able to reproduce this.
I'm using exact code and settings file from the @Alex-Nalin 's recent post. I do NOT create any files besides the main.py
and settings.yaml
. I'm using this client config to get client ID and secret:
Note: you have to specify web app client type + you must add http://localhost:8080/
to the Authorized redirect URIs
since you both use LocalWebserverAuth
workflow.
It opens the page for me, then asks a few questions, I have to force proceed against security warning since app is not verified. After that is saves the credentials.json
nearby the main.py
.
Next time I run the app - it doesn't ask anything.
Thanks @shcheklein!
It's indeed working via settings.yaml
and without the need for the credentials.json
or the client_secrets.json
.
At first run it asks for auth then saves it under credentials.json
.
A follow-up question if I may, how can multiple users run this python script, can we create multiple credentials.json
for each user? or a way to modify the settings.yaml to store them at runtime?
Thanks again.
good to hear that, @Alex-Nalin .
how can multiple users run this python script
could you please give us a bit more details about the setup? is it the same machine, and exactly same file that multiple people run?
@shcheklein I already switched to DO spaces for this project, but I will gladly create a small example this week as soon as I catch a few hours of downtime to do so!
good to hear that, @Alex-Nalin .
how can multiple users run this python script
could you please give us a bit more details about the setup? is it the same machine, and exactly same file that multiple people run?
The python script is used for a bot workflow, any user can trigger the bot to get notified of new shared files from their own Google drive, it is a company wide drive.
I am able to get it working for me but I wanted to know how to make it work for other users at the same time?
@Alex-Nalin sorry, it's still not enough details to recommend anything. How do people trigger it? Where that bot is located? How is it being run? etc
@shcheklein Can I ask will the refresh token in credentials.json
ever expire? In other words, if I use settings.yaml
, will it ever requires me to authenticate again? This is because I also need the script to run in a headless environment which doesn't have a browser. I'm thinking to just to do the first and only once authentication in my local and upload the credentials.json
to remote. Is it feasible? Please advise. Thank you in advance.
@hihaha99 sorry, I don't have a precise answer on this. This might help https://stackoverflow.com/questions/8953983/do-google-refresh-tokens-expire though.
If you use DVC built-in app then it's a verified app, there should be no problem. But if you do your custom app from that thread it might be 7 days (?) ... I would go read those links and do some experiments to confirm.
in the root of my Django project, I'm placing the
client_secret.json
andsettings.yml
, however,credentials.json
isn't getting created and saved after a successful login.Current use of the library.
Issue is that upon each request, it requires to select an account which is not acceptable in this use case. I'm using the entire thing with celery to run a tasks that uploads the file to a drive