【问题标题】:Unable to open a PDF uploaded to SharePoint using the MS Graph API(Python)无法打开使用 MS Graph API (Python) 上传到 SharePoint 的 PDF
【发布时间】:2022-10-04 23:18:54
【问题描述】:

我正在尝试使用 MS Graph API 将 PDF 上传到 SharePoint,并且我能够上传 PDF,但无法读取/预览 PDF。

我尝试将 PDF 文件转换为 Base64 以允许上传,因为它需要是二进制流,但它似乎不起作用。

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

这是我的 PUT 声明:

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)

这是我的标题:

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

任何帮助或指导表示赞赏。

谢谢

【问题讨论】:

    标签: python pdf sharepoint msgraph


    【解决方案1】:

    显然,您无需添加转换为 Base64 的步骤即可将 pdf 上传到 SharePoint。

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

    然后在 API 调用中将数据字段更新为 data=pdf_file.read() 以读取文件:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      相关资源
      最近更新 更多