requests.exceptions.HTTPError: 401 客户端错误 atlassian-python-api

时间:2021-03-01 16:47:19

标签: python confluence confluence-rest-api atlassian-python-api

我正在尝试使用 API 上的 python 包装器连接到 Confluence 页面(因为我不熟悉其中任何一个),但我不断收到以下错误:

requests.exceptions.HTTPError: 401 Client Error

我知道人们谈论这是由于必须使用 API 令牌造成的,但该页面在旧版本的 Confluence 上运行,我被告知我们不能使用访问令牌。

那么有没有人有其他想法?这是一个小代码:

from atlassian import Confluence

confluence = Confluence(
    url='https://address',
    username='name',
    password='pwd'
    )

confluence.create_page(
    space='Test',
    title='A title',
    body='something')

我曾尝试使用旧版本的 atlassian-python-api 以防万一发生冲突,但它给我带来了同样的错误。

1 个答案:

答案 0 :(得分:1)

您的代码看起来不错。使用基本身份验证对 Confluence 进行身份验证应该可以在不生成 API 令牌的情况下工作,afaik。

401 状态肯定表明身份验证存在问题。这样做的明显原因当然是错误的凭据,但我假设您已经仔细检查了凭据在使用浏览器以交互方式登录 confluence 时是否有效。

为了更好地了解错误,您可以import logging调试您的请求和响应:

from atlassian import Confluence
import logging

logging.basicConfig(filename='conf_connect.log', filemode='w', level=logging.DEBUG)

try: 
    c = Confluence(url='https://conf.yoursystem.com', username='name', password='pwd')
    # atlassian API does not raise error on init if credentials are wrong, this only happens on the first request
    c.get_user_details_by_username('name')

except Exception as e:
    logging.error(e)

Confluence 模块内部也使用 logging,因此请求和响应将出现在您的 conf_connect.log 日志文件中:

DEBUG:atlassian.rest_client:curl --silent -X GET -H 'Content-Type: application/json' -H 'Accept: application/json'  'https://conf.yoursystem.com/rest/api/user?username=name'
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): conf.yoursystem.com:443
DEBUG:urllib3.connectionpool:https://conf.yoursystem.com:443 "GET /rest/api/user?username=name HTTP/1.1" 401 751
DEBUG:atlassian.rest_client:HTTP: GET rest/api/user -> 401 
DEBUG:atlassian.rest_client:HTTP: Response text -> <!doctype html><html lang="en"><head><title>HTTP Status 401 – Unauthorized</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 401 – Unauthorized</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Basic Authentication Failure - Reason : AUTHENTICATED_FAILED</p><p><b>Description</b> The request has not been applied because it lacks valid authentication credentials for the target resource.</p><hr class="line" /><h3>Apache Tomcat/9.0.33</h3></body></html>
ERROR:root:401 Client Error:  for url: https://conf.yoursystem.com/rest/api/user?username=name

响应正文可能包含一些有关原因的信息:

<块引用>

HTTP 状态 401 – 未经授权

类型状态报告

消息基本身份验证失败 - 原因:AUTHENTICATED_FAILED

描述 请求没有被应用,因为它缺少目标资源的有效身份验证凭据。

原因 AUTHENTICATED_FAILED 表明您的凭据可能有问题。如果您想更深入地了解这一点,您还可以使用 this SO answer 来显示与您的请求一起发送的标头。

但是,如果您的原因是 AUTHENTICATION_DENIED,则问题可能如下:如果您连续多次尝试失败的身份验证,则会触发 CAPTCHA 质询,并且此错误将发生,直到 Failed登录计数已重置。当您开发脚本并经常对其进行测试时,这很容易发生。要解决此问题,请打开浏览器并手动(重新)登录 Confluence,完成验证码,或resolve it from the Confluence User Management