python - 我需要任何有使用 ArcSight esm rest API 经验的人的帮助
问题描述
我能够从登录 api 获取身份验证令牌,但我正在尝试使用它来查询事件 api,并且我收到 401 Client Error: Unauthorized for url 错误消息。这是我的代码片段:
def action():
data = {
'login': 'xxxxxxxxx',
'password': 'xxxxx',
}
urllib3.disable_warnings()
try:
timestamp = int(round(time.time() * 1000))
print(timestamp)
r = requests.post(
'https://host:port/www/core-service/rest/LoginService/login', data=data, verify=False)
login_request = untangle.parse(r.text)
user_session_id = login_request.ns3_loginResponse.ns3_return.cdata
print(user_session_id)
response = requests.post(
'https://host:port/detect-api/rest/v1/events/retrieve',
headers={
"Accept": "application/json",
"Authorization": user_session_id,
"Content-Type": "application/json"
},
data={
"ids": 79745681,
"startTime": timestamp,
"endTime": timestamp
},
verify=False)
print(response)
res = untangle.parse(response.text)
print(res)
有人可以指出我的代码有什么问题吗?
解决方案
您没有添加指向 API 的链接,所以我只能猜测会出现什么问题。
如果您设置"Content-Type": "application/json"
,则可能意味着您要发送json
数据。
所以你应该使用
post(..., json=...)
代替
post(..., data=...).
使用data=
你发送它作为form
,而不是json
。并且标头Content-Type
不能改变它。
当你使用时,json=
你不必添加"Content-Type": "application/json"
,因为requests
它会自动添加。
编辑:
在评论中你说你有工作curl
命令(但你没有说有问题,也没有显示curl
有问题)
在https://curl.trillworks.com页面上,您可以转换curl
为Python
(以及少数其他语言),并且大多数情况下它可以正常工作。
顺便提一句:
如果您使用邮递员进行测试,那么它还具有为和许多其他语言生成代码curl
的功能。Python
您没有显示 API 文档的链接,但 API 文档通常有示例curl
,有时甚至有Python
.
推荐阅读
- java - 如何删除给定 PDF 中的“TEST”,然后使用任何 Java 库保存它
- typescript - TS:断言对象的非空属性
- reactjs - Eslint 抱怨绝对进口
- javascript - 在编译前端时,它还会编译一个 .test.js 文件
- python - 我如何通过 django 中的聚合来做到这一点
- reactjs - 日历中的反应 js 和 moment.js 中的日期时间格式
- flutter - 如何获得gridview颤振的第一个单元格
- c++ - For循环没有提供正确的整数?语法错误?
- sql-server - 使用 HTML 和 Classic ASP 读取、写入和显示 Unicode 字符
- python - 如何查看账户的所有者?