使用python API获取Google云端硬盘中文件的downloadUrl属性失败(Failed in getting the downloadUrl property of files in G

编程入门 行业动态 更新时间:2024-10-28 18:32:14
使用python API获取Google云端硬盘中文件的downloadUrl属性失败(Failed in getting the downloadUrl property of files in Google Drive with python API)

我想在Google云端硬盘上获取某个文件的直接下载链接,我使用的是用于python的Google API客户端 ,这里是我的代码的一部分,它基本上是快速入门示例的副本:

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">

我搜索并阅读了有关此问题的所有相关问题。 正如一位在这个问题中所说:

此downloadURL和webContentLink之间的区别在于webContentLink使用来自用户浏览器cookie的授权,downloadURL需要授权的API请求(使用OAuth 2.0)。

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

使用Credentials类的authorize()函数将必要的凭据头应用于httplib2.Http实例发出的所有请求...一旦httplib2.Http对象被授权,它通常会传递给构建函数

那我的程序有什么问题? 如果我想用urllib写我自己的请求或重现错误的请求,怎么样?

I want to obtain the direct download link of a certain file on Google Drive and I used Google API Client for python and here's a part of my codes, which basically is the copy of the 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

Then I got the 400 error:

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

I searched and read all related questions about this problem. As one said in 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).

So I thought maybe I didn't authorize the request successfully. However the first three statements in main part, did it for me according to the 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

So what's wrong with my program? And if I want to write my own request with urllib or requests to reproduce the error, how?

最满意答案

downloadURL是一个字段,可用于Google Drive API 版本2中的文件资源,但不是您似乎使用的版本3

在Google Drive API v3中,该字段已被替换为使用?alt=media请求执行files.get直接下载文件,而无需找到该文件的特定URL。 您可以在此处阅读有关API v2和v3之间的差异和更改的更多信息。

以下是使用带有Google Drive API v3的python Google API客户端库下载文件的示例:

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)

有关更多示例,请查看有关使用API​​ v3下载文件的官方文档。

downloadURL is a field that is available for File resources in the Google Drive API version 2 but not version 3 which you seem to be using.

In Google Drive API v3 the field has been replaced by doing a files.get with ?alt=media request to directly download a file without having to find out the specific URL for it. You can read more about the differences and changes between API v2 and v3 here.

Here is an example of how you can download files using the python Google API client library with 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.

更多推荐

本文发布于:2023-07-28 21:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:云端   盘中   属性   文件   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!