(drive) API
Богомолов Иван для PyNSK
i32311113@icloud.com
Models
ClientGoogle
Your App
Server-Server
Client-Server
HowTo enable API
https://console.developers.google.com/project/ageless-valor-110414
HowTo enable API
https://console.developers.google.com/project/ageless-valor-
110414/apiui/apiview/drive/overview
Auth
https://console.developers.google.com/project/ageless-valor-110414/apiui/credential
Auth
https://console.developers.google.com/project/ageless-valor-
110414/apiui/credential/serviceaccount/390006575094-
bm1e72sjish8m7d4500b0ht1vhn4mtdn@developer.gserviceaccount.com
Start
pip install --upgrade pyOpenSSL
pip install --upgrade google-api-python-client
from datetime import date
from httplib2 import Http
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.http import MediaFileUpload
import random
Code
def 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
Examples
def 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
Object
Just example
mimeTypeapplication/vnd.google-apps.document
version13
appDataContentsFalse
thumbnailLinkhttps://docs.google.com/feeds/vt?
labels{u'restricted': False, u'starred': False,
explicitlyTrashedFalse
etag"pvTNHKA6KkAgXTpZXMwU4P
lastModifyingUserName3900065750
writersCanShareTrue
owners[{u'emailAddress': u'3900065
id1Wvm7MOpy8zFUIz69lP1y1Ck31UozmRN8yEAWHFRHULA
lastModifyingUser{u'emailAddress': u'390006575094-bm1e72sj
title2015-10-200.493659065111
ownerNames[u'390006575094-bm1e72sjish8m7d4500b0
lastViewedByMeDate2015-10-20T16:24:17.910Z
parents[{u'isRoot': True, u'kind': u'drive#parentReference'
exportLinks{u'text/html': u'https://docs.google.com/feeds
sharedTrue
description0.493659065111
editableTrue
kinddrive#file
markedViewedByMeDate1970-01-01T00:00:00.000Z
modifiedDate2015-10-20T16:24:17.910Z
createdDate2015-10-20T16:24:16.858Z
iconLinkhttps://ssl.gstatic.com/docs/doclist/images/icon_
embedLinkhttps://docs.google.com/document/d/1Wvm7
alternateLinkhttps://docs.google.com/document/d/1Wvm
copyableTrue
modifiedByMeDate2015-10-20T16:24:17.910Z
userPermission{u'kind': u'drive#permission', u'etag': u'"p
spaces[u'drive']
quotaBytesUsed0
selfLinkhttps://www.googleapis.com/drive/v2/files/1Wvm
Links
https://console.developers.google.com
https://developers.google.com/drive/web/quickstart/python

Быстрый старт в gDrive API