在Python中从Google API获取文件元数据

0 投票
1 回答
2206 浏览
提问于 2025-04-17 18:52

我现在正在尝试用Python从谷歌云盘下载文件的元数据。我基本上是从谷歌提供的文档中复制粘贴代码,之前都没什么问题。但是当我尝试调用:

drive_file = drive_service.files().get(id=file_id).execute()

时,我收到了以下错误:

drive_file = drive_service.files().get(id=file_id).execute()
File "build\bdist.win-amd64\egg\apiclient\discovery.py", line 452, in method
TypeError: Got an unexpected keyword argument "id"

不过,在谷歌的文档中,有一个示例在这里(在Python标签下),显示的内容和我写的几乎一模一样。我的代码不工作是因为我对Python不够熟悉,还是其他什么原因呢?完整的程序如下:

import httplib2

from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow

def getDriveService():
    ClientID = <MY_CLIENT_ID>
    ClientSecret = <MY_CLIENT_SECRET>
    OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
    REDIRECT_URI = <MY_REDIRECT_URI>

    flow = OAuth2WebServerFlow(ClientID, ClientSecret, OAUTH_SCOPE, REDIRECT_URI)
    flow.redirect_uri = REDIRECT_URI
    authorize_url = flow.step1_get_authorize_url()
    print authorize_url
    code = raw_input('Enter verification code: ').strip()

    credentials = flow.step2_exchange(code)          
    http = httplib2.Http()
    http = credentials.authorize(http)

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

drive_service = getDriveService()
files = drive_service.files().list().execute()

file_id = files['items'][0]['id']

#Error occurs here
drive_file = drive_service.files().get(id=file_id).execute()

print 'Title: %s' % drive_file['title']
print 'Description: %s' % drive_file['description']
print 'MIME type: %s' % drive_file['mimeType']

1 个回答

5

我解决了这个问题。
你可以修改这些代码

drive_file=drive_service.files().get(id=file_id).execute()

drive_file = drive_service.files().get(fileId=file_id).execute()

撰写回答