(drive) APIБогомолов Иван для PyNSK
Models
ClientGoogle
Your App
Server-Server
Client-Server
HowTo enable APIhttps://console.developers.google.com/project/ageless-valor-110414
HowTo enable APIhttps://console.developers.google.com/project/ageless-valor-110414/apiui/apiview/drive/overview
Authhttps://console.developers.google.com/project/ageless-valor-110414/apiui/credential
Authhttps://console.developers.google.com/project/ageless-valor-110414/apiui/credential/serviceaccount/390006575094-bm1e72sjish8m7d4500b0ht1vhn4mtdn@developer.gserviceaccount.com
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
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
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
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
Links
https://console.developers.google.com
https://developers.google.com/drive/web/quickstart/python