gpt4 book ai didi

python - 使用 Python3 更新 Sharepoint 2013

转载 作者:太空狗 更新时间:2023-10-29 21:57:46 24 4
gpt4 key购买 nike

我目前正在尝试更新 Sharepoint 2013 列表。

这是我用来完成该任务的模块。但是,当我运行发布任务时,我收到以下错误:

"b'{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"无效JSON。 JSON 内容中的 token 未被识别。"}}}'"

知道我做错了什么吗?

def update_item(sharepoint_user, sharepoint_password, ad_domain, site_url, sharepoint_listname):
login_user = ad_domain + '\\' + sharepoint_user
auth = HttpNtlmAuth(login_user, sharepoint_password)
sharepoint_url = site_url + '/_api/web/'
sharepoint_contextinfo_url = site_url + '/_api/contextinfo'
headers = {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose',
'odata': 'verbose',
'X-RequestForceAuthentication': 'true'
}
r = requests.post(sharepoint_contextinfo_url, auth=auth, headers=headers, verify=False)
form_digest_value = r.json()['d']['GetContextWebInformation']['FormDigestValue']

item_id = 4991 # This id is one of the Ids returned by the code above
api_page = sharepoint_url + "lists/GetByTitle('%s')/items(%d)" % (sharepoint_listname, item_id)
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"

}

r = requests.post(api_page, {'__metadata': {'type': 'SP.Data.TestListItem'}, 'Title': 'TestUpdated'}, auth=auth, headers=update_headers, verify=False)
if r.status_code == 204:
print(str('Updated'))
else:
print(str(r.status_code))

最佳答案

我将您的代码用于我的场景并解决了问题。

我也遇到了同样的问题。我认为更新数据的方式不正确

像下面这样传递:

json_data = {
"__metadata": { "type": "SP.Data.TasksListItem" },
"Title": "updated title from Python"
}

并将 json_data 传递给如下请求:

r= requests.post(api_page, json.dumps(json_data), auth=auth, headers=update_headers, verify=False).text    

注意:我使用了 SP.Data.TasksListItem,因为它是我的类型。使用 http://SharePointurl/_api/web/lists/getbytitle('name')/ListItemEntityTypeFullName 查找类型

关于python - 使用 Python3 更新 Sharepoint 2013,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51351647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com