Google Drive API,上传文件[Python]

时间:2014-01-04 15:38:17

标签: python api google-drive-api

我使用API​​将我的备份从服务器上传到我的谷歌硬盘。传递的授权没有问题,文件被上传,但文件没有出现在列表中。 代码:

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('1234567890@developer.gserviceaccount.com', 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)

来自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'1234567890@developer.gserviceaccount.com',
                        u'isAuthenticatedUser': True,
                        u'kind': u'drive#user',
                        u'permissionId': u'08619299632362135867'},
 u'lastModifyingUserName': u'1234567890@developer.gserviceaccount.com',
 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'1234567890@developer.gserviceaccount.com'],
 u'owners': [{u'displayName': u'1234567890@developer.gserviceaccount.com',
              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 个答案:

答案 0 :(得分:2)

只需在您的Google云端硬盘中搜索我的文档,您就会找到此上传的文件

答案 1 :(得分:1)

在Drive v3中,我们可以使用create function上传:

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

虽然我已经尝试过,但这仅适用于媒体文件类型。

API链接:

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