我正在尝试使用Python更新公司SharePoint中的某些列表项,并且过帐请求总是会导致401错误。我已经验证了凭据应该是好的,因为我可以毫无疑问地获得请求,只需查找所有列表项即可。我真的很想获得帮助,我是否知道为什么仅针对邮寄请求才会出现这种401错误?非常感谢。
这是我在Python脚本中的一些代码:
import requests, json
from requests_ntlm import HttpNtlmAuth
sharepoint_url = "sharepoint.someintranet.com/.../_api/web"
# this is to get the form digest value later when do post request
sharepoint_contextinfo_url = "sharepoint.someintranet.com/.../_api/contextinfo"
sharepoint_listname = "the_list_name"
# this account and password should be good
auth = HttpNtlmAuth("domain\\user", "password")
# header for get request
headers = {'accept': 'application/json; odata=verbose'}
# now do get request (look up some items in the list I specify)
# this get request is good; no issue here
response = requests.get(sharepoint_url+"lists/GetByTitle('%s')" % sharepoint_listname, auth=auth, headers=headers)
# ...
# now try to do post request: update some item in the list
# id of some random record
item_id = 42
api_page = sharepoint_url + "lists/GetByTitle('%s')/GetItemById(%d)" % (sharepoint_listname, item_id)
info_update = requests.post(sharepoint_contextinfo_url, auth=auth, headers=headers,)
# get form digest value first (should be good here)
form_digest_value = info_update.json()['d']['GetContextWebInformation']['FormDigestValue']
# prepare the headers for post request
update_headers = {
"Accept":"application/json; odata=verbose",
"Content-Type":"application/json; odata=verbose",
"odata":"verbose",
"X-RequestForceAuthentication": "true",
"X-RequestDigest" : form_digest_value,
"IF-MATCH": "*",
"X-HTTP-Method" : "MERGE",
}
# now try to do post request
data = json.dumps({'__metadata': { 'type': 'SP.Data.SomeListListItem' }, 'Title': 'updateIt'})
update_try = requests.post(api_page, data, auth=auth, headers=update_headers)
# the above post request will result in a 401 error
我非常确定data
在这里是正确的,因为如果我更改data
会导致400错误,而不是401。非常感谢您对为什么出现此401错误有任何见解或想法。