python - 请求异常.HTTPError : 401 Client Error atlassian-python-api

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

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

requests.exceptions.HTTPError: 401 Client Error

我知道人们谈论这是由于使用 API token 的必要性造成的,但该页面在旧版本的 Confluence 上运行,并且我被告知我们无法使用访问 token 。

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

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 以防出现一些冲突,但它给了我同样的错误。

最佳答案

你的代码看起来没问题。据我所知,使用基本身份验证对 Confluence 进行身份验证应该可以在不生成 API token 的情况下进行。

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

为了更好地了解错误,您可以导入日志记录来调试您的请求和响应:

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 模块内部也使用日志记录,因此请求和响应将显示在您的 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 Status 401 – Unauthorized

Type Status Report

Message Basic Authentication Failure - Reason : AUTHENTICATED_FAILED

Description The request has not been applied because it lacks valid authentication credentials for the target resource.

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

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

关于python - 请求异常.HTTPError : 401 Client Error atlassian-python-api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66426181/

相关文章:

python - 使用 python 发出命令提示符

javascript - 使用该行中的变量在每个表格行之后插入 HTML

oauth-2.0 - Azure : Access to https://login. microsoftonline.com/{tenant-id}/saml2 与不记名 token

java - Java 和 Python 中继承的区别

python - NLTK WordNet Lemmatizer - 如何删除未知单词?

Python 机会 : how to pass additional arguments to optimization routines

wiki - 是否可以在最新的 Confluence 中启用标记编辑器?

confluence - 你能在 Confluence 的 table-plus 宏中设置特定于单元格的背景颜色吗?

python - 在 Confluence API 中保留新行

json - 从 Confluence JSON 响应中获取值(value)