更新资产 401 Unauthorized

jumpserver :v3.6.3

     def _gen_auth(self):
        signature_headers = ["(request-target)", "accept", "date"]
        auth = HTTPSignatureAuth(
            key_id=self.KeyID,
            secret=self.SecretID,
            algorithm="hmac-sha256",
            headers=signature_headers,
        )
        return auth
    def update_host(self, host: dict, **kwargs):
        url = self.jms_url + f"/api/v1/assets/devices/{host['id']}"
        host.update(kwargs)
        print(self.auth)
        response = requests.put(
            url,
            auth=self.auth,
            headers=self.headers,
            json=host,
        )
        response.raise_for_status()
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: http://example.com:10086/api/v1/assets/devices/58cf8681-4918-4fa2-9d61-49bbb230ed02/

创建hosts是可行的,但是在更新时出现了权限错误。api key是管理员账号创建的,想知道是什么原因。

    def create_host(self, hostname, ip, **kwargs):
        url = self.jms_url + "/api/v1/assets/hosts/"
        host = self.host(name=hostname, address=ip, **kwargs)

        response = requests.post(
            url,
            auth=self.auth,
            headers=self.headers,
            json=host,
        )
        response.raise_for_status()

两个请求的auth和header都是一样的,但是post就没有这个问题。我怀疑是api key的权限问题,但是没有配置的地方。

更新资产用put /assets/host/
详细看api文档

…找到问题所在了,在url的最后少了“/”,

url = self.jms_url + f"/api/v1/assets/devices/{host['id']}/"

引以为戒

哈哈哈哈哈 这应该是看一半的问题。我之前也犯过错