大家好!我遇到了使用 OData 和 Python 在 1C 中创建项目的问题。这是我的代码:
import requests
from requests.auth import HTTPBasicAuth
import xml.etree.ElementTree as ET
post_url = 'http://localhost/mc/odata/standard.odata/Catalog_%D0%9D%D0%BE%D0%BC%D0%B5%D0%BD%D0%BA%D0%BB%D0%B0%D1%82%D1%83%D1%80%D0%B0'
data_to_post = {
"Description": "Товар на продажу",
"Code": "1234554321"
}
ns = {
'atom': 'http://www.w3.org/2005/Atom',
'm': 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
'd': 'http://schemas.microsoft.com/ado/2007/08/dataservices'
}
root = ET.Element(ET.QName(ns['atom'], 'entry'))
content = ET.SubElement(root, ET.QName(ns['atom'], 'content'), type="application/xml")
properties = ET.SubElement(content, ET.QName(ns['m'], 'properties'))
for key, value in data_to_post.items():
prop = ET.SubElement(properties, ET.QName(ns['d'], key))
prop.text = value
xml_data = ET.tostring(root, encoding="utf-8", method="xml")
headers = {'Content-Type': 'application/xml'}
response = requests.post(post_url, auth=HTTPBasicAuth(username='odata.user', password=''), data=xml_data, headers=headers)
if response.status_code == 201:
print("Data successfully posted.")
print("Response:", response.text)
else:
print(f"Failed to post data: {response.status_code}")
print("Response content:", response.text)
结果,它给出了以下错误
Failed to post data: 500
Response content: <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-1</m:code>
<m:message>Ошибка при выполнении обработчика - 'ПередЗаписью'</m:message>
</m:error>
无论我用谷歌搜索多少次,我都找不到遇到过这种情况的人。请告诉我如何解决这个问题,也许添加一些参数或者使用现成的库更容易
我发现问题出在用户权限上,系统建议创建的标准用户odata.user只能读取数据。要进行更改,您需要授予此权限(尚未尝试过),或者在可以在系统中执行相同操作的其他人的领导下工作。创建了一个新用户,名字必须是英文,否则系统会报错
我以该用户的名义提出请求