Created
April 19, 2013 15:07
-
-
Save mobedigg/5420958 to your computer and use it in GitHub Desktop.
For stackoverflow http://stackoverflow.com/questions/16011049/google-drive-api-and-service-account
small app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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) |
As I remember I used this something like this https://developers.google.com/drive/api/v3/quickstart/python or this https://developers.google.com/identity/protocols/oauth2/service-account
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to set the role of service account for this scenario?