Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google API get()与get_media()之比较_Python_Google Drive Api - Fatal编程技术网

Python Google API get()与get_media()之比较

Python Google API get()与get_media()之比较,python,google-drive-api,Python,Google Drive Api,我正在尝试使用Python访问存储在Google Team Drive上的一些文件。我已经找到了下载文件的功能,但在尝试获取元数据时遇到了一个神秘的问题 如果我执行以下操作: myfileid = 'thegooglefileid' self.service = build('drive', 'v3', http=creds.authorize(Http())) data = self.service.files().get_media(fileId=myfileid).execute() me

我正在尝试使用Python访问存储在Google Team Drive上的一些文件。我已经找到了下载文件的功能,但在尝试获取元数据时遇到了一个神秘的问题

如果我执行以下操作:

myfileid = 'thegooglefileid'
self.service = build('drive', 'v3', http=creds.authorize(Http()))
data = self.service.files().get_media(fileId=myfileid).execute()
meta = self.service.files().get(fileId=myfileid,fields="*").execute()
“数据”按预期返回,允许我按预期下载文件。“meta”返回一个HttpError 404,表示它找不到文件(实际上它在上面一行中找到了该文件)

我知道,如果授权设置不正确,可能会出现此问题,但我的授权设置为我希望它能够正常工作

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
          'https://www.googleapis.com/auth/drive',
          'https://www.googleapis.com/auth/drive.file']

您知道为什么该文件对API的一部分可见而对另一部分不可见吗?

在这种情况下,一个重要的问题是访问TeamDrive。在检索“meta”的get调用的特定情况下,需要通知API它正在使用TeamDrive

一旦我弄明白了这一点,下面的代码就可以工作了

myfileid = 'thegooglefileid'
self.service = build('drive', 'v3', http=creds.authorize(Http()))
data = self.service.files().get_media(fileId=myfileid).execute()
meta = self.service.files().get(fileId=myfileid,fields="*",supportsTeamDrives=True).execute()

有趣的是,get需要这个参数,而get_媒体没有这个参数就可以正常工作

虽然我不确定这是否是错误的直接原因,但在您的脚本中,
myfilename
而不是
myfileid
被用作
fileId
。谢谢。我从真实的代码中编辑了这个,以删除实际的文件id,我的修改很差。我编辑了原文,感谢您的回复。我没注意到这件事。我很抱歉,嗨。您是如何找到下载该文件的方法的?