Transcript
Page 1: Быстрый старт в gDrive API

(drive) APIБогомолов Иван для PyNSK

[email protected]

Page 2: Быстрый старт в gDrive API

Models

ClientGoogle

Your App

Server-Server

Client-Server

Page 3: Быстрый старт в gDrive API

HowTo enable APIhttps://console.developers.google.com/project/ageless-valor-110414

Page 4: Быстрый старт в gDrive API

HowTo enable APIhttps://console.developers.google.com/project/ageless-valor-110414/apiui/apiview/drive/overview

Page 5: Быстрый старт в gDrive API

Authhttps://console.developers.google.com/project/ageless-valor-110414/apiui/credential

Page 6: Быстрый старт в gDrive API

Authhttps://console.developers.google.com/project/ageless-valor-110414/apiui/credential/serviceaccount/390006575094-bm1e72sjish8m7d4500b0ht1vhn4mtdn@developer.gserviceaccount.com

Page 7: Быстрый старт в gDrive API

Startpip install --upgrade pyOpenSSLpip install --upgrade google-api-python-client

from datetime import datefrom httplib2 import Httpfrom apiclient.discovery import buildfrom oauth2client.client import SignedJwtAssertionCredentialsfrom apiclient.http import MediaFileUploadimport random

Page 8: Быстрый старт в gDrive API

Codedef auth():

client_email='390006575094-bm1e72sjish8m7d4500b0ht1vhn4mtdn\@developer.gserviceaccount.com'

with open('My Project-f5de14b11059.p12', 'r') as f:private_key=f.read()

credentials =SignedJwtAssertionCredentials(client_email,\private_key, 'https://www.googleapis.com/auth/drive')http =Http()credentials.authorize(http)service =build('drive', 'v2', http=http)return service

Page 9: Быстрый старт в gDrive API

Examplesdef show(service):

Files =\service.files().list().execute()

forf infiles['items']:print f['title'], f['id']

def add(service, content):id =str(random.random())withopen('temp', 'w') asf:

f.write(content)body ={'title':str(date.today()) +id,\'description':id,\'mimeType':'text/plain'\}media_body=MediaFileUpload('temp', mimetype='text/plain',\

resumable=True)f =service.files().insert(body=body, media_body=media_body,\

convert=True).execute()permission ={ 'value':'','type':'anyone','role':'writer' }files =service.files().list().execute()forf infiles['items']:

try:p =service.permissions().insert(fileId=f['id'],\

body=permission).execute()except:

pass

Page 10: Быстрый старт в gDrive API

ObjectJust example

mimeTypeapplication/vnd.google-apps.documentversion13appDataContentsFalsethumbnailLinkhttps://docs.google.com/feeds/vt?labels{u'restricted': False, u'starred': False,explicitlyTrashedFalseetag"pvTNHKA6KkAgXTpZXMwU4PlastModifyingUserName3900065750writersCanShareTrueowners[{u'emailAddress': u'3900065id1Wvm7MOpy8zFUIz69lP1y1Ck31UozmRN8yEAWHFRHULAlastModifyingUser{u'emailAddress': u'390006575094-bm1e72sjtitle2015-10-200.493659065111ownerNames[u'390006575094-bm1e72sjish8m7d4500b0lastViewedByMeDate2015-10-20T16:24:17.910Zparents[{u'isRoot': True, u'kind': u'drive#parentReference'exportLinks{u'text/html': u'https://docs.google.com/feedssharedTrue

description0.493659065111editableTruekinddrive#filemarkedViewedByMeDate1970-01-01T00:00:00.000ZmodifiedDate2015-10-20T16:24:17.910ZcreatedDate2015-10-20T16:24:16.858ZiconLinkhttps://ssl.gstatic.com/docs/doclist/images/icon_embedLinkhttps://docs.google.com/document/d/1Wvm7alternateLinkhttps://docs.google.com/document/d/1WvmcopyableTruemodifiedByMeDate2015-10-20T16:24:17.910ZuserPermission{u'kind': u'drive#permission', u'etag': u'"pspaces[u'drive']quotaBytesUsed0selfLinkhttps://www.googleapis.com/drive/v2/files/1Wvm

Page 11: Быстрый старт в gDrive API

Links

https://console.developers.google.com

https://developers.google.com/drive/web/quickstart/python


Recommended