Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

How to download a file shared with me from SharePoint or OneDrive?

Open pietrogalvagni opened this issue 3 years ago • 0 comments

Hello @vgrem, I was able to download a file from my personal SharePoint following the ReadMe (create credential client_id and client_secret), but now I need to download a file shared with me by another account. The file is shared within my organization (so it's not public). Is that possible?

My current code is the following

class SharePoint:

    def auth(self):
        cred = ClientCredential(CLIENT_ID, CLIENT_SECRET)
        ctx = ClientContext(URL_SITE).with_credentials(cred)
        return ctx

    def downloadFile(self, fileName, folderName):
        conn = self.auth()
        file = conn.web.get_file_by_guest_url(SHAREPOINT_URL).execute_query()
        print(file.name)

        with open(f"{LOCAL_FOLDER}/{file.name}", "wb") as f:
            file.download(f).execute_query()

If SHAREPOINT_URL is generated from a file in my drive, the code works fine. If it comes from a file shared with me, it doesn't work. The following exception arise:

ClientRequestException ('-2130575338, Microsoft.SharePoint.SPException', 'The file |0 does not exist.', '404 Client Error: Not Found for url: https://xxx.sharepoint.com/personal/pietro_galvagni_xxx_com/_api/Web/GetFileByGuestUrl')

Do you know a way to get the file? Thank you

pietrogalvagni avatar Aug 10 '22 13:08 pietrogalvagni