最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Changing access option of a file using Google Drive API - Stack Overflow

programmeradmin3浏览0评论

Is it possible to change the access option of a file in Drive from 'Restricted' to 'Anyone with the Link' using Google Drive API?

filename= 'filename' file_metadata = { 'name' : filename, 'parents' : [PARENT_FOLDER_ID], 'type':'anyone', 'role':'reader' }

I have tried tweaking the 'type' parameter however that doesn't seem to be related to the issue.

Is it possible to change the access option of a file in Drive from 'Restricted' to 'Anyone with the Link' using Google Drive API?

filename= 'filename' file_metadata = { 'name' : filename, 'parents' : [PARENT_FOLDER_ID], 'type':'anyone', 'role':'reader' }

I have tried tweaking the 'type' parameter however that doesn't seem to be related to the issue.

Share Improve this question asked 2 days ago kyourin_forkyourin_for 231 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Yes, you can change the access option of a file from 'Restricted' to 'Anyone with the Link' using the Google Drive API. You'll need to use the permissions endpoint to modify file sharing settings.# Create permission for "Anyone with the link"

# Create permission for "Anyone with the link"
permission = {
    'type': 'anyone',  # Allows access to anyone
    'role': 'reader',  # Gives read access
    'allowFileDiscovery': False  # Makes it accessible only via link
}

# Create the permission
service.permissions().create(
    fileId=file_id,
    body=permission,
    fields='id'
).execute()

If you want to update an existing permission instead of creating a new one, you can use the permissions.update endpoint. https://developers.google/drive/api/reference/rest/v3/permissions/update

Change file sharing permission

To change the sharing permission of a file using Drive API, use Method: permissions.create with the following request body:

{
  "role": "writer",
  "type": "anyone"
}

I tried this using the API Explorer and got the desired result.

Output

Before using the api

After using the api

References:

Permissions.create method

发布评论

评论列表(0)

  1. 暂无评论