我正在尝试编写一个可以访问sharepoint并将excel文件上传到它的python脚本。我已经设法达到通过身份验证并检索FormDigestValue的程度,但我似乎无法正确获取上传命令。到目前为止,这是我的代码:
import requests
from requests_ntlm import HttpNtlmAuth
#get formdigestvalue
headers = {'accept': 'application/json;odata=verbose'}
r = requests.post("https://sharepoint.example.com/example/_api/contextinfo",auth=HttpNtlmAuth('username', 'password'), 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://sharepoint.example.com/example/_api/web/GetFolderByServerRelativeUrl('Trainings/Training Records/JohnSmith')/files/add(url=""test.xlsx"", overwrite='true')"
excelfile = {'file': ('test.xlsx', open('test.xlsx', 'rb'), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
auth=HttpNtlmAuth('username', 'password')
p = requests.post(posturl, headers=POSTheaders, files = excelfile, auth = auth)
print p.status_code
print p.content
我从中获得的输出如下:
200
0x63B311022FB07B3B7D7751B0B06C2EF0232DDACD9255DF871FD7599E08A2A014B06ED22DE55246C7F0C7459D8717E347631390502ED7556C6019BB69C7745EA0,15 Feb 2017 12:50:17 -0000
400
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The expression \"web/GetFolderByServerRelativeUrl('Trainings/Training Records/JohnSmith')/files/add(url=test.xlsx, overwrite='true')\" is not valid."}}}
不幸的是,错误信息没有提供太多关于错误的详细信息,无论我如何调整帖子请求,我都无法通过它。关于我做错的任何想法都将不胜感激。