[pygerrit2]HTTPError: 401 客户端错误: 未授权 url:.



我正在使用pygerrit2向我的gerrit服务器进行身份验证并遇到以下错误,用户名和密码是正确的,因为我使用相同的用户名和密码从gerrit UI手动登录,为什么我会收到此错误以及如何解决它?

from pygerrit2 import GerritRestAPI, HTTPBasicAuth
auth = HTTPBasicAuth('username', 'password')
print auth
rest = GerritRestAPI(url='https://tech.company.com', auth=auth)
changes = rest.get("/changes/?q=owner:self%20status:open")

错误:-

Traceback (most recent call last):
File "get_related_changes.py", line 62, in <module>
changes = rest.get("/changes/?q=owner:self%20status:open")
File "/Library/Python/2.7/site-packages/pygerrit2/rest/__init__.py", line 156, in get
decoded_response = _decode_response(response)
File "/Library/Python/2.7/site-packages/pygerrit2/rest/__init__.py", line 51, in _decode_response
response.raise_for_status()
File "/Library/Python/2.7/site-packages/requests/models.py", line 939, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://tech.company.com/a/changes/?q=owner:self%20status:open

首先,请确保您为 Gerrit 版本使用了正确的身份验证类型。 默认情况下,2.14 之前的版本使用摘要身份验证;2.14 及更高版本使用基本。

其次,请确保您使用正确的密码。它应该是用户设置的"HTTP 密码"部分中的那个。

这可能会有所帮助,我通过从配置文件中读取用户名和密码解决了类似的问题。

尝试:

# config.ini
[my_app]
USERNAME = enter_username
PASSWORD = enter_password

从 Python 3.8 为 Python 2.6+ 安装配置解析器:pip install configparser。 有关更多详细信息,请参阅出色的文档。

from pygerrit2 import GerritRestAPI, HTTPBasicAuth
import configparser
config = configparser.RawConfigParser()
config.read('config.ini')
my_user = config.get('my_app', 'USERNAME')
my_pass = config.get('my_app', 'PASSWORD')
auth = HTTPBasicAuth(my_user, my_pass)
print auth
rest = GerritRestAPI(url='https://tech.company.com', auth=auth)
changes = rest.get("/changes/?q=owner:self%20status:open")

最新更新