首页 > 解决方案 > 我需要任何有使用 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)

有人可以指出我的代码有什么问题吗?

标签: pythonapiresteventsarcsight

解决方案


您没有添加指向 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页面上,您可以转换curlPython(以及少数其他语言),并且大多数情况下它可以正常工作。

顺便提一句:

如果您使用邮递员进行测试,那么它还具有为和许多其他语言生成代码curl的功能。Python

您没有显示 API 文档的链接,但 API 文档通常有示例curl,有时甚至有Python.


推荐阅读