Search code examples
pythonpython-requestsgoogle-drive-api

How to make shared folder on google drive using python requests?


I have a function that makes shared files it works perfectly for normal files but when I need to create a public folder it can`t. In Google documentation they are same operations

def GetLinkToFile(file_id,file2):
    url = 'https://www.googleapis.com/drive/v3/files/' + file_id + '/permissions?supportsAllDrives=true'
    headers = {'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json'}
    payload = {'type': 'anyone', 'role': 'reader'}
    res = requests.post(url, data=payload, headers=headers)
    link = file2['alternateLink']
    pyperclip.copy(link)

This function returns error 400 Invalid request when used with folder and works fine when used with file


Solution

  • I think that the request for your showing script is required to be modified. So, I would like to propose the following modification.

    From:

    res = requests.post(url, data=payload, headers=headers)
    

    To:

    res = requests.post(url, data=json.dumps(payload), headers=headers)
    
    • In this case, please add import json.

    Reference: