5
votes

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}
2
You say the file is uploaded, you mean that you see it in Drive using a browser? I ask, because I don't know what you mean by "does not appear in the list". Which list? Moreover, as I answered to another person, why don't you use PyDrive? - Nil
No, i'm doesn't see file in google drive using a browser. But API returns 'ok response' with link to file, and this file not available. - user1277254
Ok, I can't help you with this specific problem, but I still suggest you use, or at least try, PyDrive. I'm pretty sure it will solve your problem. - Nil
You are using a Service Account, so the file is in the drive of this account. If you want to be able to see it, you should give writer/reader permission to your user and then it will appear in your "Shared with me" drive folder - Ruben Bermudez
hi this code is work fine for me. Just passed fourth parameter user_email(For the person in which drive we want to upload file) in SignedJwtAssertionCredentials. - Poonam Gokani

2 Answers

2
votes

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

1
votes

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