如何使用服务帐户从谷歌驱动器文件下载或读取内容



我可以使用带有访问令牌的服务帐户列出谷歌驱动器中的所有文件/docsREST APIPython脚本并且我无法下载/读取文档中的内容。它的投掷错误如下。

https://www.googleapis.com/drive/v2/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC

获取文件数据API URL

{
"kind": "drive#file",
"id": "1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC",
"etag": ""MTU4NzI4MTM3MzQ2Ng"",
"selfLink": "https://www.googleapis.com/drive/v2/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC",
"webContentLink": "https://drive.google.com/a/dummywebsite.com/uc?id=1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC&export=download",
"alternateLink": "https://drive.google.com/a/dummywebsite.com/file/d/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC/view?usp=drivesdk",
"embedLink": "https://drive.google.com/a/dummywebsite.com/file/d/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC/preview?usp=drivesdk",
"iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/text/plain",
"thumbnailLink": "https://lh3.googleusercontent.com/ydrbZst2W6we3faCPlnYYKluglSTNprdrhEbJjueA5lRgjsqG8FN-Xp0Zqyy68H_XOTEWi2yQlU=s220",
"title": "hello.txt",
"mimeType": "text/plain",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},
"copyRequiresWriterPermission": false,
"createdDate": "2020-04-17T20:34:32.643Z",
"modifiedDate": "2020-04-19T07:29:33.466Z",
"modifiedByMeDate": "2020-04-17T20:34:30.000Z",
"lastViewedByMeDate": "2020-04-18T20:09:44.394Z",
"markedViewedByMeDate": "1970-01-01T00:00:00.000Z",
"version": "5",
"parents": [
{
"kind": "drive#parentReference",
"id": "1ccTLIrnwkbTZxUoUavCIwCmz5c68Rymi",
"selfLink": "https://www.googleapis.com/drive/v2/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC/parents/1ccTLIrnwkbTZxUoUavCIwCmz5c68Rymi",
"parentLink": "https://www.googleapis.com/drive/v2/files/1ccTLIrnwkbTZxUoUavCIwCmz5c68Rymi",
"isRoot": false
}
],
"downloadUrl": "https://www.googleapis.com/drive/v2/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC?alt=media&source=downloadUrl",
"userPermission": {
"kind": "drive#permission",
"etag": ""Oui67PMpWNMX2al1jEJ17kn5eZg"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC/permissions/me",
"role": "owner",
"type": "user"
},
"originalFilename": "hello.txt",
"fileExtension": "txt",
"md5Checksum": "8ed250d6af95636c70b3ed064260246e",
"fileSize": "4",
"quotaBytesUsed": "4",
"ownerNames": [
"Balasubramanian V"
],
"owners": [
{
"kind": "drive#user",
"displayName": "Balasubramanian V",
"picture": {
"url": "https://lh3.googleusercontent.com/a-/AOh14GiIt3jyOQ6VYF0XK_RjwnFTihxnXHOTzzRlaXlRrETHSP79k3XpKMVpX5ZctbtDdThSt_8cxbr2TKWf-BkHe7DzzIyYD4Pz0Et8G_lPv0t7Vk63-sh_JSAZIh6OJbUqzvNh3721AEhxd3wqVMAZT1ux8QIbsMaOrHaSLqGut1XTmqbKUyhfUiLRKUZWTtEb3ZFsggG5G6T-zyxITU65HuXZX5f5pQwoKSf8hyRHIeJBR04ofxltrV6v5jRteBaHUB8ZS6MUe_Gw1Lar4hVTNW5Cy5vAu-b9elgLSvtqNLC9RGP_U0ILlBemgj72rtz2WBzPoeoiRZum_2uASihttAISspHq1L4Ks5L_KFZ2O80YPZ4mfVdywn1L4jlQf0uAXIEhe2jlEjyPf1eQ2LrsrA1ApkxgXkwNSHPvjU4fUcXNpefucf9TrNIX8VRbu4DPHKT8C3EdqpFx5dQLh-m7EvJE1H0TTZ5nY6mhHfFebeC14W22uk95Tg953y9M1PDiYyIC-hlcNLUWSeOx3CBzCMGAAaUfQqO5fyvOU-3gAENPO1qdPnurPdEsuH5csDx6tuyMZSHcGOPTR_5FHp6rcQ-gjtahI0pHqB27Bt0aeg7JuTG4vSA7v7ru6DK6Rb73jP0rEoDztNW72K-CqtpLjM4kGn30ZhaLM8qHNbiD_zGjZT5_8oldj6ad4eKn_DrQuC51vDEIzyrYwkEf_E9wBR7rlVUiWIqsFHBALIyw9z_aW3122BAdePAvotGQ3vlrGjRXTw=s64"
},
"isAuthenticatedUser": true,
"permissionId": "02978886251107598035",
"emailAddress": "info@dummywebsite.com"
}
],
"lastModifyingUserName": "Balasubramanian V",
"lastModifyingUser": {
"kind": "drive#user",
"displayName": "Balasubramanian V",
"picture": {
"url": "https://lh3.googleusercontent.com/a-/AOh14GiIt3jyOQ6VYF0XK_RjwnFTihxnXHOTzzRlaXlRrETHSP79k3XpKMVpX5ZctbtDdThSt_8cxbr2TKWf-BkHe7DzzIyYD4Pz0Et8G_lPv0t7Vk63-sh_JSAZIh6OJbUqzvNh3721AEhxd3wqVMAZT1ux8QIbsMaOrHaSLqGut1XTmqbKUyhfUiLRKUZWTtEb3ZFsggG5G6T-zyxITU65HuXZX5f5pQwoKSf8hyRHIeJBR04ofxltrV6v5jRteBaHUB8ZS6MUe_Gw1Lar4hVTNW5Cy5vAu-b9elgLSvtqNLC9RGP_U0ILlBemgj72rtz2WBzPoeoiRZum_2uASihttAISspHq1L4Ks5L_KFZ2O80YPZ4mfVdywn1L4jlQf0uAXIEhe2jlEjyPf1eQ2LrsrA1ApkxgXkwNSHPvjU4fUcXNpefucf9TrNIX8VRbu4DPHKT8C3EdqpFx5dQLh-m7EvJE1H0TTZ5nY6mhHfFebeC14W22uk95Tg953y9M1PDiYyIC-hlcNLUWSeOx3CBzCMGAAaUfQqO5fyvOU-3gAENPO1qdPnurPdEsuH5csDx6tuyMZSHcGOPTR_5FHp6rcQ-gjtahI0pHqB27Bt0aeg7JuTG4vSA7v7ru6DK6Rb73jP0rEoDztNW72K-CqtpLjM4kGn30ZhaLM8qHNbiD_zGjZT5_8oldj6ad4eKn_DrQuC51vDEIzyrYwkEf_E9wBR7rlVUiWIqsFHBALIyw9z_aW3122BAdePAvotGQ3vlrGjRXTw=s64"
},
"isAuthenticatedUser": true,
"permissionId": "02978886251107598035",
"emailAddress": "info@dummywebsite.com"
},
"capabilities": {
"canCopy": true,
"canEdit": true
},
"editable": true,
"copyable": true,
"writersCanShare": true,
"shared": true,
"explicitlyTrashed": false,
"appDataContents": false,
"headRevisionId": "0B_QZJ9jWm1W-OXZCZEpkOG1HT2Q4alBrWmRPdDI4aXNQWnI4PQ",
"spaces": [
"drive"
]

}

API URL:https://www.googleapis.com/drive/v3/files/1LWkJmHqv7lHfdd6qLq8ssdJ99BYeP-9axC/export?mimeType=text/plain

{
"error": {
"errors": [
{
"domain": "global",
"reason": "fileNotExportable",
"message": "Export only supports Google Docs."
}
],
"code": 403,
"message": "Export only supports Google Docs."
} }

我不想通过浏览器身份验证下载文档。我需要它通过服务器端的自动下载和阅读过程。

所有在线指南都只提供网络下载选项。

请帮帮我。

如果我理解正确,您只想下载文件。如果是这种情况,您只需要使用方法get_media()

如果您不使用库,只需将?alt=media添加到您的请求中,就可以下载文件而不是元数据。

这是一个目前对我有效的例子。

GET /drive/v3/files/<file_id>?alt=media HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer <access_token>

来自谷歌官方文档中有关在Drive中下载文件的信息。

要下载存储在Google Drive上的文件,请使用files.get方法,其中包含要下载的文件的ID和alt=media URL参数。alt=media URL参数告诉服务器正在请求下载内容。

相关内容

  • 没有找到相关文章