1

I'm using pydrive library in order to get the shareable link of a video that I uploaded in a shared google drive folder, but I get the download link instead.

Here is part of my code:

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))
Sandra
  • 13
  • 4

1 Answers1

2

I believe your goal as follows.

  • You want to retrieve the shared link of the folders like https://drive.google.com/drive/folders/{folderId}?usp=sharing.

In the current stage, it seems that Drive API cannot directly return the shared link. So in this case, I think that the shared link can be created using the folder ID retrieved with drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList().

When your script is modified, it becomes as follows.

Modified script:

From:
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']
To:
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']
  • In this sample modification, when the folder id publicly shared, the shared link is returned.

Note:

  • For example, if you want to retrieve the shared link of the files except for Google Docs files (Document, Spreadsheet, Slides and so on), you can use https://drive.google.com/file/d/{fileId}/view?usp=sharing.

Reference:

Added:

  • You want to retrieve the shared link (view link) from the uploaded files in the specific folder.

In this case, I think that alternateLink can be used. But in your updated script, from {'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}, the folders of folderName are retrieved. So it is also required to modify the search query.

Modified script:

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))
  • In your script, I think that folderId can be used from folderId = folder['id'].
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for your help. I tried to adapt the code using https://drive.google.com/file/d/file[id]/view?usp=sharing but I get an error message about there's no preview available. – Sandra Sep 17 '20 at 01:01
  • @Sandra Thank you for replying. I apologize for the inconvenience. From `files = drive.ListFile({'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()` of your script, I had thought that you might want to retrieve the shared link of the folder. This is due to my poor English skill. I deeply apologize for this. – Tanaike Sep 17 '20 at 01:10
  • @Sandra About the shared link of the file, `https://drive.google.com/file/d/{fileId}/view?usp=sharing` can be used when the file is publicly shared. So in order to correctly understand about `I get an error message about there's no preview available.`, can you add your current script for replicating the issue in you question? By this, I would like to confirm it. – Tanaike Sep 17 '20 at 01:11
  • I edited my question, so I put my current code. I think that the error could be a missing attribute of the item. I already read the google drive API document but I can't find that attribute that shows the preview of the video. – Sandra Sep 17 '20 at 01:26
  • @Sandra Thank you for replying adding more information. From your updated question, I added one more sample script. Could you please confirm it? If that was not the result you expect, I apologize again. – Tanaike Sep 17 '20 at 02:22
  • I've got another error: couldn't process the video. I'll continue trying and I'll tell you. – Sandra Sep 17 '20 at 03:26
  • @Sandra Thank you for replying. I apologize for the inconvenience. At first, can I ask you about the proposed script worked? Because from your replying, I cannot understand about it. I apologize for my poor English skill. If the file size is large, it might be required to wait. So at first, as a test of the script, please test it using the file with the small size. By the way, in my environment, I could confirm that no error occurs. – Tanaike Sep 17 '20 at 04:14
  • The purpose of the script was achieved and I would like to thank you for your help and patient!. Beside this, I have other issues that I'll write in another post. Thank you so much! – Sandra Sep 17 '20 at 15:30
  • @Sandra Thank you for your response. – Tanaike Sep 18 '20 at 01:27
  • I hope you are well, here is my new question: https://stackoverflow.com/questions/63960334/uploading-a-video-with-a-shareable-link-using-pydrive-but-it-doesnt-process – Sandra Sep 18 '20 at 17:30