简体   繁体   English

使用驱动器 API V3 上传更新的带有 Python 的 Google 驱动器上的文件不起作用

[英]Upload updated a file on Google Drive with Python using Drive API V3 does not work

I would not post this here but I invested hours now into understanding why the last part of my Python script does not behave as I expect it to behave reading documentation and many other posts on this topic.我不会在这里发布这个,但我现在花了几个小时来理解为什么我的 Python 脚本的最后一部分没有像我预期的那样表现阅读文档和有关此主题的许多其他帖子。

What I want to achieve.我想要达到的目标。

  • Create project in Google Cloud Platform and enable Drive API - works在 Google Cloud Platform 中创建项目并启用 Drive API - 有效
  • Create OAuth credentials for Drive API - works为驱动器 API 创建 OAuth 凭据 - 有效
  • Authenticate with Google Drive from Python script - works从 Python 脚本使用 Google Drive 进行身份验证 - 有效
  • Download the file I want to edit with Python script - works下载我想用 Python 脚本编辑的文件 - 有效
  • Upload the modified file after edit with Python script - does not work使用 Python 脚本编辑后上传修改后的文件 - 不起作用

I am using this function from the official Google documentation: https://developers.google.com/drive/api/v2/reference/files/update我正在使用 Google 官方文档中的 function: https://developers.google.com/drive/api/v2/reference/files/update

But it gives the following error:但它给出了以下错误:

    raise TypeError('Got an unexpected keyword argument "%s"' % name)
TypeError: Got an unexpected keyword argument "newRevision"

When I remove it from the function call I am getting this:当我从 function 调用中删除它时,我得到了这个:

An error occurred: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files/1T1dl_Z3z-FDLa7lbWib8XxgbTd5pMpIP?alt=json&uploadType=resumable returned "The resource body includes fields which are not directly writable.">发生错误:<HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files/1T1dl_Z3z-FDLa7lbWib8XxgbTd5pMpIP?alt=json&uploadType=resumable返回“资源正文包含不可直接写入的字段。” >

I do not understand why I am getting this.我不明白为什么我会得到这个。

Why does the official sample contain a parameter that is not supported?为什么官方示例中包含不支持的参数? Is it possible that I am using a different version of the Google Python libraries locally then they expect me to have?我是否有可能在本地使用不同版本的 Google Python 库,然后他们希望我拥有? I just followed the official documentation again?我刚刚又按照官方文档了? Is is possible that I do not have sufficient rights to update the file?是否有可能我没有足够的权限来更新文件?

Thanks, Andre谢谢,安德烈

Meanwhile I figured it out by myself why it was not working.同时,我自己弄清楚了为什么它不起作用。

If you follow the Python Google Drive Quick Onboarding you will use version 3 of their API: https://developers.google.com/drive/api/v3/quickstart/python如果您遵循 Python Google Drive Quick Onboarding,您将使用其 API 的版本 3: https://developers.google.com/drive/api/v3

This line defines the version:这一行定义了版本:

service = build('drive', 'v3', credentials=creds)

If you continue searching in their documentation for a functionality to update a file I ended up here in the version 2 documentation and snippets:如果您继续在他们的文档中搜索更新文件的功能,我最终在第 2 版文档和片段中找到了:

https://developers.google.com/drive/api/v2/reference/files/update https://developers.google.com/drive/api/v2/reference/files/update

Looking here for the version 3 snippet there is none existing in their documentation: https://developers.google.com/drive/api/v3/reference/files/update在这里寻找版本 3 片段,他们的文档中不存在: https://developers.google.com/drive/api/v3/reference/files/update

So I assumed it is still valid what they posted for version 2, but this is not the case.所以我认为他们为第 2 版发布的内容仍然有效,但事实并非如此。

I did everything now in version 2 and it is finally working.我现在在第 2 版中做了所有事情,它终于可以工作了。

Edit to add reference:编辑以添加参考:

newRevision is a valid parameter in v2, but not in v3, so I assume that, in v3, blob uploads always create a new revision (that's what happens in v2 if this parameter is not set – default is true ). newRevision在 v2 中是一个有效参数,但在 v3 中不是,所以我假设在 v3 中,blob 上传总是会创建一个新修订版(如果未设置此参数,则在 v2 中会发生这种情况——默认值为true )。 See Parameter differences between v2 and v3 :请参阅v2 和 v3 之间的参数差异

+--------------+--------------+--------------+
|    Method    | v2 Parameter | v3 Parameter |
+--------------+--------------+--------------+
| files.update | newRevision  |      n/a     |
+--------------+--------------+--------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌驱动器 API v3 使用 Python - Google Drive API v3 using Python google Drive api v3 文件上传错误 - google Drive api v3 file upload errors 使用gzip通过API v3 Python 3优化上传到云端硬盘 - Using gzip to Optimize Upload to Drive using API v3 Python 3 使用 Python 中的 next_chunk() 上传 Google Drive API v3 错误查看状态的说明? - Explaination on error viewing status of Google Drive API v3 upload using next_chunk() in Python? 谷歌驱动器API v3(python)更新文件权限“角色” - google drive api v3 (python) update file premissions “role” 我如何使用谷歌驱动器 api v3 将文件上传到共享驱动器 - How can i used google drive api v3 to upload a file to shared drive 在Python 3上使用Drive API v3恢复上传并显示上传百分比 - Resumable Upload and Show Percentage of Upload using Drive API v3 on Python 3 Google Drive API v3 更改文件权限并获取可公开共享的链接 (Python) - Google Drive API v3 Change File Permissions and Get Publicly Shareable Link (Python) 如何在 Python 中使用 Google API v3 使驱动器文件夹“对网络公开”? - How to make drive folder "public to the web" using Google API v3 in Python? googleapiclient.errors.HttpError 与 google drive v3 api 上传错误 - googleapiclient.errors.HttpError with google drive v3 api upload error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM