简体   繁体   English

如何在 Python 中使用 Google API v3 使驱动器文件夹“对网络公开”?

[英]How to make drive folder "public to the web" using Google API v3 in Python?

I can make a folder using:我可以使用以下方法创建文件夹:

from Google import Create_Service

CLIENT_SECRET_FILE = "credentials_gsuite.json"
API_NAME = "drive"
API_VERSION ="v3"
SCOPES = ["https://www.googleapis.com/auth/drive"]
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
folder_name = "Folder"

file_metadata = {"name" : folder_name, "mimeType" : "application/vnd.google-apps.folder"}
service.files().create(body=file_metadata,).execute()

And i need to make folder "public to the web" .而且我需要将文件夹设为“对网络公开”。 In order to do that i need to specify permisions:为此,我需要指定权限:

'role': 'reader

and a type to:和一个类型:

'type': 'anyone'

and to allow:并允许:

"allowFileDiscovery": True

judging by the documentation this should work:从文档来看,这应该有效:

    from Google import Create_Service
    
    CLIENT_SECRET_FILE = "credentials_gsuite.json"
    API_NAME = "drive"
    API_VERSION ="v3"
    SCOPES = ["https://www.googleapis.com/auth/drive"]
    service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
    folder_name = "Folder"
    
    file_metadata = {"name" : folder_name, "mimeType" : "application/vnd.google-apps.folder", 'role': 'reader', 'type': 'anyone',"allowFileDiscovery": True}
    service.files().create(body=file_metadata,).execute()

But im not getting it published on to the web.但我没有把它发布到网上。 What am I missing?我错过了什么?

Documentation: https://developers.google.com/drive/api/v3/reference/permissions文档: https : //developers.google.com/drive/api/v3/reference/permissions

Modification points:改装要点:

  • Unfortunately, at the method of service.files().create() , it seems that permissions is not writable.不幸的是,在service.files().create()的方法中,似乎permissions不可写。 When {"mimeType":"application/vnd.google-apps.folder","name":"sample","permissions":[{"role":"reader","type":"anyone","allowFileDiscovery":true}]} is used as the request body of "Files: create", an error like The resource body includes fields which are not directly writable.{"mimeType":"application/vnd.google-apps.folder","name":"sample","permissions":[{"role":"reader","type":"anyone","allowFileDiscovery":true}]}作为"Files: create"的请求体,出现类似The resource body includes fields which are not directly writable.的错误The resource body includes fields which are not directly writable. occurs.发生。 So in this case, it is required to use the method of "Permissions: create" after the new folder was created.所以在这种情况下,需要在新建文件夹后使用“权限:创建”的方法。

When this is reflected to your script, it becomes as follows.当这反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

folder_name = "Folder"
file_metadata = {"name": folder_name, "mimeType": "application/vnd.google-apps.folder"}
folder = service.files().create(body=file_metadata,).execute()
folderId = folder.get('id')

permission = {
    'role': 'reader',
    'type': 'anyone',
    'allowFileDiscovery': True
}
res = service.permissions().create(fileId=folderId, body=permission).execute()
print(res)

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌驱动器 API v3 使用 Python - Google Drive API v3 using Python 使用驱动器 API V3 上传更新的带有 Python 的 Google 驱动器上的文件不起作用 - Upload updated a file on Google Drive with Python using Drive API V3 does not work 谷歌驱动器API v3(python)更新文件权限“角色” - google drive api v3 (python) update file premissions “role” 使用 Python 中的 next_chunk() 上传 Google Drive API v3 错误查看状态的说明? - Explaination on error viewing status of Google Drive API v3 upload using next_chunk() in Python? 使用gzip通过API v3 Python 3优化上传到云端硬盘 - Using gzip to Optimize Upload to Drive using API v3 Python 3 如何使用 google drive v3 api 调用请求进行身份验证 - How do I authenticate using requests for google drive v3 api calls 我如何使用谷歌驱动器 api v3 将文件上传到共享驱动器 - How can i used google drive api v3 to upload a file to shared drive Google Drive API v3 更改文件权限并获取可公开共享的链接 (Python) - Google Drive API v3 Change File Permissions and Get Publicly Shareable Link (Python) 无法与共享驱动器和文件交互 | Python 谷歌驱动器 API v3 - Cannot Interact with Shared Drives & Files | Python Google Drive API v3 google Drive api v3 文件上传错误 - google Drive api v3 file upload errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM