如何在 Google Drive API v3 中更新权限? [英] How to update permissions in Google Drive API v3?

查看:33
本文介绍了如何在 Google Drive API v3 中更新权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用服务帐户上传了 wav 文件,服务帐户创建了文件,服务帐户是所有者:

metadata = {'name': 文件名, 'parents': [FolderId]}media = MediaFileUpload(filepath, mimetype='audio/wav')r = drive.files().create(body=metadata, media_body=media, fields='id').execute(){'权限':[{'删除':假,'displayName': '我的名字','emailAddress': 'myemail@gmail.com','id': '00654354190098938408','kind': '驱动#权限','photoLink': 'https://lh6.googleusercontent.com/photo.jpg','角色':'作家','类型':'用户'},{'删除':错误,'displayName': 'myservice@myservice.iam.gserviceaccount.com','emailAddress': 'myservice@myservice.iam.gserviceaccount.com','id':'16815597635264162472','kind': '驱动#权限','角色':'所有者','类型':'用户'}]}

然后我尝试将 myemail 帐户的 rolewriter 更改为 owner 并获得一个错误:

drive.permissions().update(fileId=fileId,permissionId='00654354190098938408', transferOwnership=True,body={'role': 'owner'}).execute()

<块引用>

用户对该文件没有足够的权限.

怎么了?

我的服务账户是所有者,我可以通过服务账户删除文件,但它没有权限转移所有权,为什么?当我使用 service 帐户创建电子表格并将所有权转移到 myemail 帐户时,它运行良好.

我尝试创建新权限并获得此权限:

权限 = {电子邮件地址":myemail@gmail.com",角色":所有者",类型":用户",}drive.permissions().create(fileId=fileId, body=permission, transferOwnership=True).execute()

<块引用>

"错误的请求.用户消息:您还不能更改此项目的所有者.(我们正在努力.)"

解决方案

根据此处找到的文档 让其他人成为您文件的所有者

<块引用>

从您的个人帐户,您可以传输以下文件类型:

  • Google 文档
  • Google 表格
  • Google 幻灯片
  • Google 表单
  • 谷歌绘图
  • Google 我的地图
  • 文件夹提示:当您转移文件夹的所有权时,它只会转移选定的文件夹,不包括其中的文件.

如果 Google Drive 不支持传输该文件类型,那么服务帐户也将无法传输它.

解决方法.让用户将文件上传到他们的帐户,而不是服务帐户.

I uploaded wav file using service account, service account created the file, service account is the owner:

metadata = {'name': filename, 'parents': [FolderId]}
media = MediaFileUpload(filepath, mimetype='audio/wav')
r = drive.files().create(body=metadata, media_body=media, fields='id').execute()

{'permissions': [{'deleted': False,
              'displayName': 'My name',
              'emailAddress': 'myemail@gmail.com',
              'id': '00654354190098938408',
              'kind': 'drive#permission',
              'photoLink': 'https://lh6.googleusercontent.com/photo.jpg',
              'role': 'writer',
              'type': 'user'},
             {'deleted': False,
              'displayName': 'myservice@myservice.iam.gserviceaccount.com',
              'emailAddress': 'myservice@myservice.iam.gserviceaccount.com',
              'id': '16815597635264162472',
              'kind': 'drive#permission',
              'role': 'owner',
              'type': 'user'}]}

Then I'm trying to change the role of myemail account from writer to owner and get an error:

drive.permissions().update(fileId=fileId, permissionId='00654354190098938408', transferOwnership=True,
    body={'role': 'owner'}).execute()

The user does not have sufficient permissions for this file.

What is wrong?

My service account is the owner, I CAN delete the file through the service account and it does not have permissions to transfer ownership, why? When I create a spreadsheet with service account and transfer ownership to myemail account it works perfectly.

I tried to create new permission and got this:

permission = {
    "emailAddress": 'myemail@gmail.com',
    "role": 'owner',
    "type": 'user',
}
drive.permissions().create(fileId=fileId, body=permission, transferOwnership=True).execute()

"Bad Request. User message: "You can't yet change the owner of this item. (We're working on it.)""

解决方案

As per the documentation found here Make someone else the owner of your file

From your personal account, you can transfer the following file types:

  • Google Docs
  • Google Sheets
  • Google Slides
  • Google Forms
  • Google Drawings
  • Google My Maps
  • Folders Tip: When you transfer ownership of a folder it transfers only the selected folder and does not include the files inside.

If google drive doesn't support transferring that file type then the service account isnt going to be able to transfer it either.

Workaround. Have the user upload the file to their account and not the service account.

这篇关于如何在 Google Drive API v3 中更新权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆