英文:
Transfer google sheets file ownership from service account to domain email address with gspread
问题
In the example code provided, you are trying to transfer ownership of a Google Sheets file from a service account to a user with an @domain.com
email address. However, it appears that this operation is failing with the error message:
APIError: {'code': 403, 'message': 'The target user cannot be a pending owner on this file.', 'errors': [{'message': 'The target user cannot be a pending owner on this file.', 'domain': 'global', 'reason': 'targetUserCannotBePendingOwnerOnFile'}]}
This error suggests that transferring ownership to a user with an @domain.com
email address might not be supported using gspread, or there might be certain restrictions. To address this issue, you may need to use an alternative method for transferring ownership of a Google Sheets file.
If you cannot achieve this using gspread, a potential alternative could be to use the Google Sheets API directly. You can use the Google Sheets API to manage access permissions and transfer ownership of a sheet. However, this approach might involve more complex code than gspread.
Please refer to the official Google Sheets API documentation for guidance on how to handle access permissions and ownership transfer for Google Sheets files.
Remember to handle any necessary authentication and authorization when using the Google Sheets API, as it requires proper setup and permissions.
英文:
In the example code below, I perform the following:
- connect to my google service account
- copy one of the google sheets files the service account has access to
- share that copy with a user specified by
EMAIL
- transfer ownership of the copy to the user specified by
EMAIL
This code works as expected when EMAIL
is an @gmail.com
address. When the email address is an @domain.com
address (that is still using gmail, just not the @gmail address), the .transfer_ownership()
line (step 4
) fails.
Reproducible example:
# Step 1: Connect to your service account
gc = gspread.service_account("insert_path_to_credentials.json")
# Please replace this string with the spreadsheet id of any sheet the service account can access
KEY = "########"
# Step 2: Copy the spreadsheet
ss_copy = gc.copy(file_id=KEY)
# Please replace this string with the email address you wish to share with
EMAIL = "emailaddress@domain.com"
# Step 3: Share the copy with the user specified by EMAIL
response = ss_copy.share(email_address=EMAIL,
perm_type="user",
role="writer",
notify=True)
# Step 4: Transfer ownership of the copy to the user specified by EMAIL
# This fails if EMAIL is an @domain.com address where domain may
# be the name of some business that uses gmail, but address is not @gmail
permission_id = response.json()["id"]
transfer_response = ss_copy.transfer_ownership(permission_id)
Error during Step 4
when EMAIL
is an @domain.com
address:
APIError: {'code': 403, 'message': 'The target user cannot be a pending owner on this file.', 'errors': [{'message': 'The target user cannot be a pending owner on this file.', 'domain': 'global', 'reason': 'targetUserCannotBePendingOwnerOnFile'}]}
What is the correct method of using gspread to transfer ownership of a google sheet from a service account to a person using an @domain.com
email address?
If this cannot be done using gspread, what is the simplest method to transfer ownership of a google sheets file from a service account to any arbitrary email address?
答案1
得分: 1
你目前遇到的问题与API或Gspread库无关,而是与Google Drive的工作方式有关,因为你无法将文件所有权转移到不同的域。你可以在此文档中查看此信息:
已知限制和替代方案:
你不能将所有权转让给外部用户,如个人Google帐户或其他组织的用户。此限制是为了保护用户和公司数据免受未经授权的转移和访问。根据你的组织共享政策,你可能可以使用以下替代方案。
解决方法将取决于你是否能够访问Workspace帐户(@domain.com
电子邮件地址)。
如果你可以访问Workspace帐户,可以与用户共享由服务帐户拥有的文件。之后,在该域下创建一个新的服务帐户,模拟用户,并复制文件。你可以在GSpread中查看如何复制的示例这里
或将文件添加到“共享文件”中,这两种替代方案都可以在相同的文档中找到。
英文:
The problem you are currently having is not related to the API or the Gspread library. The problem is with how Google Drive Works since you cannot transfer the file ownership to a different domain. You can review this information in this Documentation:
> Known limitations and alternatives:
>
> You can't transfer ownership to or
> from an external user, such as a personal Google Account or a user in
> another organization. This limitation is to protect user and company
> data from unauthorized transfer and access. Depending on your
> organization’s sharing policies, you may be able to use the following
> alternatives.
The workarounds will depend if you have access to the Workspace account (@domain.com
email address) or not.
If you have access to the Workspace account, you can share the file owned by the services account with the user. After that, create a new services account under the domain, impersonate the user, and make a copy of the file. You can see an example of making a copy in GSpread here
Or add the file to a Share File
, both of those alternatives can be found in the same documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论