【问题标题】:Update a file with google drive API Error 403使用 google drive API 错误 403 更新文件
【发布时间】:2021-01-23 12:41:33
【问题描述】:

我目前使用 Google Cloud 的 GWT 应用程序存在问题。

我实际上有一段代码可以让您使用 google drive 应用程序上传文件。 除了这里,随着谷歌的更新,谷歌驱动器上的文件不再可能有多个父母,我们可以在这里看到:

https://cloud.google.com/blog/products/g-suite/simplifying-google-drives-folder-structure-and-sharing-models

" 您只能为还没有父项的项目添加父项。这会影响 children.insert (v2)、files.update (v2 / v3) 和 parents.insert (v2) 端点。您可以使用新的 canAddMyDriveParent 功能来检查项目当前是否没有父项,以及用户是否有足够的权限为该项目添加父项。"

所以我有以下代码:

service.files().update(fileId, file).execute();

产生错误:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Increasing the number of parents is not allowed",
    "reason" : "cannotAddParent"
  } ],
  "message" : "Increasing the number of parents is not allowed"
}

但我不知道如何上传文件,而不会出现此错误。

事实上,我不知道如何创建一个新的父文件夹,或者我是否可以在没有父文件夹的情况下上传。 如果有人可以帮助我,任何帮助将不胜感激!

非常感谢!

【问题讨论】:

  • 什么文件?您只能添加一位家长。
  • 我只能将 1 个文件添加到 1 个父级?或者我只能为我的文件设置 1 个父级?编辑:当我这样做时:service.files().list() 它打印:{} 但是,当我打印我的文件时,我有很多信息,比如“父母”:[{“id”:“anyId”,“isRoot” :true,"kind":"drive#parentReference","parentLink":"googleapis.com/drive/v2/files/…"},{"id":"anyId2"}]
  • 谷歌驱动器只是文件存储系统。一个文件只能有一个父目录,或者它会位于两个不同的位置。可以将父级设置为多个文件的父级,这意味着它有多个子级。父母是单数。
  • @DaImTo 我怎样才能创建一个新的父母?或者在其他地方更新我的文件?
  • 您需要创建一个新文件夹。 developers.google.com/drive/api/v3/folder

标签: google-app-engine google-cloud-platform gwt google-drive-api


【解决方案1】:

在今年年初(2020 年)Google 推出 shortcuts 时,API 发生了变化:

您只能为还没有父项的项目添加父项。这会影响 children.insert (v2)、files.update (v2 / v3) 和 parents.insert (v2) 端点。您可以使用新的 canAddMyDriveParent 功能来检查项目当前是否没有父项,以及用户是否有足够的权限为该项目添加父项。

这是在 3 月宣布的,但从 9 月 30 日开始实施。

您猜对了:Shortcuts 是在不同文件夹中复制文件的方法,但要考虑到它们与原始文件具有不同的特征并且它们不是“副本”。 MimeType 是application/vnd.google-apps.shortcut .

快捷方式创建示例:

file_metadata = {
    'name': 'Project Plan',
    'mimeType': 'text/plain'
}
file = drive_service.files().create(body=file_metadata, fields='id').execute()
print('File ID: %s' % file.get('id'))
shortcut_metadata = {
     'Name': 'Shortcut to Project Plan',
     'mimeType': 'application/vnd.google-apps.shortcut',
     'shortcutDetails': {
        'targetId': file.get('id')
     }
}
shortcut = drive_service.files().create(body=shortcut_metadata,
                                    fields='id,shortcutDetails').execute()
print('File ID: %s, Shortcut Target ID: %s, Shortcut Target MIME type: %s' % (
    shortcut.get('id'),
    shortcut.get('shortcutDetails').get('targetId'),
    shortcut.get('shortcutDetails').get('targetMimeType')))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多