Skip to content

Instantly share code, notes, and snippets.

@mobedigg
Created April 19, 2013 15:07
Show Gist options
  • Save mobedigg/5420958 to your computer and use it in GitHub Desktop.
Save mobedigg/5420958 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import SignedJwtAssertionCredentials
# Email of the Service Account.
SERVICE_ACCOUNT_EMAIL = '@developer.gserviceaccount.com'
# Path to the Service Account's Private Key file.
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '-privatekey.p12'
def createDriveService():
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope='https://www.googleapis.com/auth/drive')
http = httplib2.Http()
http = credentials.authorize(http)
return build('drive', 'v2', http=http)
FILENAME = 'document.txt'
drive_service = createDriveService()
media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
body = {
'title': 'My document',
'description': 'A test document',
'mimeType': 'text/plain'
}
file = drive_service.files().insert(body=body, media_body=media_body).execute()
pprint.pprint(file)
@tianchao-haohan
Copy link

How to set the role of service account for this scenario?

@mobedigg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment