'Using Python to do basic Google Drive operations without web server authentication
I want to create a Python script that can do some really basic things (add/remove files in a shared google drive) The script will be running on remote PCs so anything involving web authentication is off the table
I read online that using a service account was the way to go, so I did the following:
Created a project
Enabled the Google Drive API for the project
Created a service account for the project (left "Grant this service account access to project", couldn't figure out what, if anything, I should put here)
Created a key for the service account, downloaded it and tried to use it the following way:
service_account_info = json.load(open('service_account.json')) SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'', "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"] creds = service_account.Credentials.from_service_account_info(service_account_info, scopes=SCOPES) service = build('drive', 'v3', credentials=creds) results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', [])
Which results in an exception after my "results" line executes:
google.auth.exceptions.RefreshError: ('No access token in response.', {'id_token': '[longtoken'})
I saw something that hinted I needed to use the "OAuth 2.0 Playground" where I tick off "Use your own Oauth credentials", specify the ClientID and ClientSecret and authorize the Google Drive APIs but when I try to do that I Just get "Error 400: redirect_uri_mismatch" (and I'm not sure if resolving this will actually get me anywhere)
Is there a viable way of doing what I want to do?
Solution 1:[1]
You're missing these steps:
- Setting up Oauth consent
- Create OAuth Client ID Credentials for a Desktop App using the OAuth Consent
- Tunnel a remote port from your server to your local Machine
Service account is possible but it is also possible to authenticate on a remote machine -see here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 4th_haim_sister |