使用 Python 从 Google API 获取文件元数据

Luc*_*asS 0 python google-api google-drive-api

我目前正在尝试研究如何使用 python 从谷歌驱动器下载文件元数据。我几乎只是从谷歌提供的文档中复制/粘贴,到目前为止一切都很好。当我尝试打电话时:

drive_file = drive_service.files().get(id=file_id).execute()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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"
Run Code Online (Sandbox Code Playgroud)

然而,在谷歌文档中有一个例子(在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']
Run Code Online (Sandbox Code Playgroud)

Kar*_*itz 5

我解决它。您可以修改代码

drive_file=drive_service.files().get(id=file_id).execute()
Run Code Online (Sandbox Code Playgroud)

drive_file = drive_service.files().get(fileId=file_id).execute()
Run Code Online (Sandbox Code Playgroud)