尝试检查我的 gitlab-ci.yml(版本 13.8)时获取 401 Unauthorized

Get à 401 Unauthorized when trying to lint my gitlab-ci.yml (version 13.8)

从星期一开始,我无法使用 gitlab 的 lint CI API,记录在此处 https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration

我正在开发一个自托管的 gitlab,我们将 gitlab 更新到最新版本 (13.8.4)。

我注意到文档在 13.8 和 13.9 之间发生了变化,他们提到了

Access to this endpoint requires authentication.

所以我尝试生成具有完全访问权限的个人访问令牌(我是管理员),但我仍然收到 401。

这是我的尝试:

$ curl --header "Content-Type: application/json" --header "PRIVATE-TOKEN: P3r50Na1t0k3N" "https://my-domain.artips.fr/api/v4/ci/lint" --data '{"content": "{ \"image\": \"ruby:2.6\", \"services\": [\"postgres\"], \"before_script\": [\"bundle install\", \"bundle exec rake db:create\"], \"variables\": {\"DB_NAME\": \"postgres\"}, \"types\": [\"test\", \"deploy\", \"notify\"], \"rspec\": { \"script\": \"rake spec\", \"tags\": [\"ruby\", \"postgres\"], \"only\": [\"branches\"]}}"}'

# Result : {"message":"401 Unauthorized"}

# Other try

$ curl -X POST --header "Content-Type: application/json" --header "PRIVATE-TOKEN: P3r50Na1t0k3N" "https:///my-domain.artips.fr/api/v4/ci/lint" --data '{"content": "{ \"image\": \"ruby:2.6\", \"services\": [\"postgres\"], \"before_script\": [\"bundle install\", \"bundle exec rake db:create\"], \"variables\": {\"DB_NAME\": \"postgres\"}, \"types\": [\"test\", \"deploy\", \"notify\"], \"rspec\": { \"script\": \"rake spec\", \"tags\": [\"ruby\", \"postgres\"], \"only\": [\"branches\"]}}"}'

# same result: {"message":"401 Unauthorized"}


有没有人运行遇到同样的问题?

提前致谢

用户令牌范围必须向您的 gitlab 实例的 API 授予 read/write 权限(如图所示)。用户不必是管理员。

关于 CI Lint API 端点有一个 ongoing issue。在本地 Gitlab 实例上禁用“注册”时,此端点的身份验证似乎不起作用。我想这个问题会在未来的版本中得到解决。

import requests
import urllib.parse
from requests.auth import HTTPBasicAuth

url = 'https://gitlab.example.com/'
api = urllib.parse.urljoin(url, '/api/v4/projects')
private_token = 'xxx'
params = dict(private_token=private_token)
auth = HTTPBasicAuth('username', 'password')  # username and password, different from login account, generally provided from ops
response = requests.get(api, params, auth=auth)
print(response)
print(response.json())