Skip to content Skip to sidebar Skip to footer

Pydrive And Google Drive - Automate Verification Process?

I'm trying to use PyDrive to upload files to Google Drive using a local Python script which I want to automate so it can run every day via a cron job. I've stored the client OAuth

Solution 1:

You can (should) create a service account - with an id and private key from the google API console - this won't require re verification but you'll need to keep the private key private.

Create a credential object based on the google python example 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 fileid = "...@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)

EDIT (Sep 2016): For the latest integrated google-api-python-client (1.5.3) you would use the following code, with id and key the same as before:

importStringIO
from apiclient import discovery
from oauth2client.service_accountimportServiceAccountCredentials

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)

Post a Comment for "Pydrive And Google Drive - Automate Verification Process?"