PyDrive and Google Drive - automate the verification process?

I'm trying to use PyDrive to upload files to Google Drive using a local Python script that I want to automate, so it can be run every day with a cron job. I saved the client's OAuth identifier and secret for the Google Drive application in the settings.yaml file locally, which PyDrive chooses to use for authentication.

The problem I get is that, although it works for a while, everyone so often decides that I need to provide a verification code (if I use CommandLineAuth), or it will lead me to the browser to enter the Google account password (LocalWebserverAuth), so I can not automate the process properly.

Does anyone know what settings I need to configure - either in PyDrive or on the Google OAuth side - to install this once and then trust it to automatically start without further user input in the future?

Here is what the settings.yaml file looks like:

client_config_backend: settings
client_config:
  client_id: MY_CLIENT_ID
  client_secret: MY_CLIENT_SECRET

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: False

oauth_scope:
  - https://www.googleapis.com/auth/drive.file
+4
source share
1 answer

You can (should) create a service account - with an identifier and a private key from the Google API console - this will not require re-verification, but you need to keep the private secret key.

Create a python google credential object and assign it to the PyDrive GoogleAuth () object:

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

# from google API console - convert private key to base64 or load from file
id = "...@developer.gserviceaccount.com"
key = base64.b64decode(...)

credentials = SignedJwtAssertionCredentials(id, key, scope='https://www.googleapis.com/auth/drive')
credentials.authorize(httplib2.Http())

gauth = GoogleAuth()
gauth.credentials = credentials

drive = GoogleDrive(gauth)

( 2016): google-api-python-client (1.5.3) , :

import StringIO
from apiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_p12_keyfile_buffer(id, StringIO.StringIO(key), scopes='https://www.googleapis.com/auth/drive')
http = credentials.authorize(httplib2.Http())
drive = discovery.build("drive", "v2", http=http)
+7

Source: https://habr.com/ru/post/1532796/


All Articles