首页 > 解决方案 > 尝试使用 python 脚本将文件上传到共享点时出现 JSONDecode 错误

问题描述

我一直在尝试将 excel 文件上传到共享点文件夹结构,其代码如下

import requests
from requests_ntlm import HttpNtlmAuth
headers = {'accept': 'application/json;odata=verbose'}
r = requests.post("https://xyz.sharepoint.com/_api/contextinfo", auth=HttpNtlmAuth('abc@xyz.com', 'pass123'), headers=headers)
print (r.status_code)
FormDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
print (FormDigestValue)

file upload
POSTheaders = {'Accept':'application/json; odata=verbose','Content-Type':'application/json;
odata=verbose','X-RequestDigest':FormDigestValue, 'binaryStringRequestBody':'true'}

posturl =  "https://xyz.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/sites/fol1/fol2')/files/add(url='pro\\sprints\\Sprint3_Test_Report.xlsx', overwrite='true')"

excelfile = {'file': ('pro\\sprints\\Sprint3_Test_Report.xlsx', open('pro\\sprints\\Sprint3_Test_Report.xlsx', 'rb'), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}

auth=HttpNtlmAuth('abc@xyz.com', 'pass123')

p = requests.post(posturl, headers=POSTheaders, files = excelfile, auth = auth)
print (p.status_code)
print (p.content)

它向我抛出 JSONDecodeError 额外数据:第 1 行第 5 列(字符 4)

我哪里错了?谢谢

标签: pythonjsonexcelhttpsharepoint

解决方案


推荐阅读