无法获取 Google 驱动器 python API 中文件的 downloadUrl 属性

Failed in getting the downloadUrl property of files in Google Drive with python API

我想直接下载link某个文件到Google驱动器,我用GoogleAPI客户端python 这是我的一部分代码,基本上是 quickstart example:

的副本
SCOPES = "https://www.googleapis.com/auth/drive"
FILE_ID = "xxxxxx"


def get_credentials():
    ...
    return credentials

if __name__ == '__main__':
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build("drive", "v3", http=http)

    web_link = service.files().get(fileId=FILE_ID, fields="webContentLink").execute() # success
    download_link = service.files().get(fileId=FILE_ID, fields="downloadUrl").execute() # throw an error

然后我得到了 400 错误:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files/xxxxxx?alt=json&fields=downloadUrl returned "Invalid field selection downloadUrl">

我搜索并阅读了所有关于这个问题的相关问题。正如 this question 中所说:

The difference between this downloadURL and the webContentLink is that the webContentLink uses authorization from the user's browser cookie, the downloadURL requires an authorized API request (using OAuth 2.0).

所以我想也许我没有成功授权请求。然而,主要部分的前三个陈述是根据 guide:

为我做的

Use the authorize() function of the Credentials class to apply necessary credential headers to all requests made by an httplib2.Http instance ... Once an httplib2.Http object has been authorized, it is typically passed to the build function

所以我的程序有什么问题?还有如果我想用urllib写自己的request或者requests重现错误,怎么办?

downloadURL 是 Google 驱动器 API 版本 2 中文件资源可用的字段,但 版本 3 您似乎正在使用。

在 Google 驱动器 API v3 中,该字段已被替换为 files.get?alt=media 请求直接下载文件而无需找出具体文件URL 为之。 You can read more about the differences and changes between API v2 and v3 here.

这是一个示例,说明如何使用 python Google API 客户端库和 Google Drive API v3 下载文件:

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)

Check out the official documentation regarding downloading files using the API v3 here for more examples.