Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API, uploading file [Python]

I'm use API for upload my backup from server to my google drive. Authorization passed without problems and file is uploaded, but the file does not appear in the list. Code:

import httplib2
import pprint
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.http import MediaFileUpload

f = file('privatekey.p12', 'rb')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials('[email protected]', key, scope='https://www.googleapis.com/auth/drive')
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

media_body = MediaFileUpload('/path/to/file/document.txt', 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)

Response from api:

{u'alternateLink': u'https://docs.google.com/file/d/0B-FWSwzP0SeyamY1MXFIMmFDZWc/edit?usp=drivesdk',
 u'appDataContents': False,
 u'copyable': True,
 u'createdDate': u'2014-01-04T14:41:19.108Z',
 u'description': u'A test document',
 u'downloadUrl': u'https://doc-0c-6s-docs.googleusercontent.com/docs/securesc/376up7hhina7i2kr3lb8jjr3i1qgs9i8/hbgdu1q3abhdnhdr75jrpjohg4aphvci/1388844000000/08619299632362135867/08619299632362135867/0B-FWSwzP0SeyamY1MXFIMmFDZWc?h=16653014193614665626&e=download&gd=true',
 u'editable': True,
 u'etag': u'"G9loKy74Mg0FQ-YRqtCj_yTTrpg/MTM4ODg0NjQ3OTAwMw"',
 u'fileExtension': u'',
 u'fileSize': u'5',
 u'iconLink': u'https://ssl.gstatic.com/docs/doclist/images/icon_10_text_list.png',
 u'id': u'0B-FWSwzP0SeyamY1MXFIMmFDZWc',
 u'kind': u'drive#file',
 u'labels': {u'hidden': False,
             u'restricted': False,
             u'starred': False,
             u'trashed': False,
             u'viewed': True},
 u'lastModifyingUser': {u'displayName': u'[email protected]',
                        u'isAuthenticatedUser': True,
                        u'kind': u'drive#user',
                        u'permissionId': u'08619299632362135867'},
 u'lastModifyingUserName': u'[email protected]',
 u'lastViewedByMeDate': u'2014-01-04T14:41:19.003Z',
 u'md5Checksum': u'ad0234829205b9033196ba818f7a872b',
 u'mimeType': u'text/plain',
 u'modifiedByMeDate': u'2014-01-04T14:41:19.003Z',
 u'modifiedDate': u'2014-01-04T14:41:19.003Z',
 u'originalFilename': u'My document',
 u'ownerNames': [u'[email protected]'],
 u'owners': [{u'displayName': u'[email protected]',
              u'isAuthenticatedUser': True,
              u'kind': u'drive#user',
              u'permissionId': u'08619299632362135867'}],
 u'parents': [{u'id': u'0AOFWSwzP0SeyUk9PVA',
               u'isRoot': True,
               u'kind': u'drive#parentReference',
               u'parentLink': u'https://www.googleapis.com/drive/v2/files/0AOFWSwzP0SeyUk9PVA',
               u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B-FWSwzP0SeyamY1MXFIMmFDZWc/parents/0AOFWSwzP0SeyUk9PVA'}],
 u'quotaBytesUsed': u'5',
 u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B-FWSwzP0SeyamY1MXFIMmFDZWc',
 u'shared': False,
 u'title': u'My document',
 u'userPermission': {u'etag': u'"G9loKy74Mg0FQ-YRqtCj_yTTrpg/ebrUqOkKZ6bmVEtr5zEJa5EOB38"',
                     u'id': u'me',
                     u'kind': u'drive#permission',
                     u'role': u'owner',
                     u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B-FWSwzP0SeyamY1MXFIMmFDZWc/permissions/me',
                     u'type': u'user'},
 u'webContentLink': u'https://docs.google.com/uc?id=0B-FWSwzP0SeyamY1MXFIMmFDZWc&export=download',
 u'writersCanShare': True}
like image 834
vladpalchikov Avatar asked Oct 20 '22 17:10

vladpalchikov


2 Answers

Just search for My document in your google drive , you will find this uploaded files

like image 142
Mateen Kadwaikar Avatar answered Oct 23 '22 11:10

Mateen Kadwaikar


In Drive v3, we could upload using the create function:

A = service.files().create(media_body = 'pig.png',body = {'name':'pig'}).execute()

Although I have tried and this only works for media file types.

API link:

https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html

like image 24
Nam Avatar answered Oct 23 '22 09:10

Nam