[英]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.我想要达到的目标。
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 版中做了所有事情,它终于可以工作了。
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.