【问题标题】:Google drive API: get the shareable publicly link of a video uploadedGoogle drive API:获取上传视频的可共享公开链接
【发布时间】:2020-09-16 22:22:07
【问题描述】:

我正在使用 pydrive 库来获取我上传到共享 google drive 文件夹中的视频的可共享链接,但我得到的是下载链接。

这是我的代码的一部分:

folderName = 'Videos'  # Please set the folder name.

folders = drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
for folder in folders:
    if folder['title'] == folderName:
        folderId = folder['id']

import glob, os
os.chdir("C:/upload_recording/videos")
for file in glob.glob("*.mp4"):
    with open(file,"r") as f:
        fn = os.path.basename(f.name)
        file_drive = drive.CreateFile({'title':fn,'parents': [{'id': folderId}], 'copyRequiresWriterPermission': True, 'writersCanShare': False})
        file_drive.Upload()
        file_drive.InsertPermission({
                        'type': 'anyone',
                        'value': 'anyone',
                        'role': 'reader'})
        
files = drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
for file in files:
    keys = file.keys()
    if file['shared']:
        link = 'https://drive.google.com/file/d/' + file['id'] + '/view?usp=sharing'
    else:
        link = 'No Link Available. Check your sharing settings.'

    name = file['id']
    
    print('name: {}  link: {}'.format(name, link))

【问题讨论】:

    标签: python permissions google-drive-api pydrive


    【解决方案1】:

    我相信你的目标如下。

    • 您要检索文件夹的共享链接,例如https://drive.google.com/drive/folders/{folderId}?usp=sharing

    在当前阶段,Drive API 似乎无法直接返回共享链接。所以在这种情况下,我认为可以使用drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()检索到的文件夹ID来创建共享链接。

    当你的脚本被修改后,变成如下。

    修改脚本:

    从:
    for file in files:
        keys = file.keys()
        if 'webContentLink' in keys:
            link = file['webContentLink']
        elif 'webViewLink' in keys:
            link = file['webViewLink']
        else:
            link = 'No Link Available. Check your sharing settings.'
    
        if 'name' in keys:
            name = file['name']
        else:
            name = file['id']
    
    到:
    for file in files:
        keys = file.keys()
        if file['shared']:
            link = 'https://drive.google.com/drive/folders/' + file['id'] + '?usp=sharing'
        elif 'webContentLink' in keys:
            link = file['webContentLink']
        elif 'webViewLink' in keys:
            link = file['webViewLink']
        else:
            link = 'No Link Available. Check your sharing settings.'
    
        if 'name' in keys:
            name = file['name']
        else:
            name = file['id']
    
    • 在本次修改示例中,当文件夹id公开共享时,返回共享链接。

    注意:

    • 例如,如果您要检索除 Google Docs 文件(文档、电子表格、幻灯片等)之外的文件的共享链接,您可以使用https://drive.google.com/file/d/{fileId}/view?usp=sharing

    参考:

    补充:

    • 您想从特定文件夹中的上传文件中检索共享链接(查看链接)。

    在这种情况下,我认为可以使用alternateLink。但是在您更新的脚本中,从{'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"} 检索folderName 的文件夹。所以也需要修改搜索查询。

    修改脚本:

    folderId = '###'  # Please set the folder ID.
    
    files = drive.ListFile({"q": "'" + folderId + "' in parents and mimeType!='application/vnd.google-apps.folder'"}).GetList()
    for file in files:
        keys = file.keys()
        if file['shared'] and 'alternateLink' in keys:
            link = file['alternateLink']
        else:
            link = 'No Link Available. Check your sharing settings.'
    
        name = file['id']
    
        print('name: {}  link: {}'.format(name, link))
    
    • 在您的脚本中,我认为folderId 可以从folderId = folder['id'] 使用。

    【讨论】:

    • 感谢您的帮助。我尝试使用 drive.google.com/file/d/file[id]/view?usp=sharing 调整代码,但我收到一条错误消息,提示没有可用的预览。
    • @Sandra 感谢您的回复。我带来的不便表示歉意。从您的脚本的files = drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList(),我认为您可能想要检索文件夹的共享链接。这是因为我的英语水平不好。对此我深表歉意。
    • @Sandra 关于文件的共享链接,公开共享文件时可以使用https://drive.google.com/file/d/{fileId}/view?usp=sharing。所以为了正确理解I get an error message about there's no preview available.,你可以添加你当前的脚本来复制你的问题吗?借此,我想确认一下。
    • 我编辑了我的问题,所以我放了我当前的代码。我认为该错误可能是该项目的缺失属性。我已经阅读了 google drive API 文档,但找不到显示视频预览的属性。
    • @Sandra 感谢您回复并添加更多信息。从您更新的问题中,我又添加了一个示例脚本。你能确认一下吗?如果这不是您期望的结果,我再次道歉。
    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多