简体   繁体   中英

Unable to open a PDF uploaded to SharePoint using the MS Graph API(Python)

I am trying to upload a PDF to SharePoint using the MS Graph API and I am able to upload a PDF, but the PDF can't be read/previewed.

I've tried converting the PDF file to Base64 to allow it to be uploaded since it needs to be a binary stream but it doesn't seem to work.

with open(pdf_filename,"rb") as pdf_file:
    pdf_base64 = base64.b64encode(pdf_file.read())

Here is my PUT statement:

response_upload = requests.put(f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:/Document Folder/PDF_Name.pdf:/content", data=pdf_base64, headers=headers)

Here are my headers:

headers = {'Authorization': 'Bearer {}'.format(token), "Content-Type":"application/pdf"}

Any help or guidance is appreciated.

Thanks

Apparently you don't need to add a step to convert to Base64 to upload a pdf to SharePoint.

with open(pdf_filename,"rb") as pdf_file:

and then in the API call updating the data field to data=pdf_file.read() to read the file:

response_upload = requests.put(f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:/Document Folder/PDF_Name.pdf:/content", data=pdf_file.read(), headers=headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM