你的代码看起来不错。使用 Basic Auth 对 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
表明您的凭据可能有问题。如果您想深入研究,您可以使用此 SO 答案来显示与您的请求一起发送的标头。
但是,如果您的原因是AUTHENTICATION_DENIED
,问题可能如下:如果您连续多次失败的身份验证尝试,则会触发 CAPTCHA 质询,并且此错误将发生,直到重置失败登录计数。当您开发脚本并经常对其进行测试时,这很容易发生。要解决此问题,请打开浏览器并手动(重新)登录到 Confluence,完成验证码,或者从 Confluence 用户管理中解决它。