Python api 意外请求 gitlab returns 空结果

Python api request to gitlab unexpectedly returns empty result

import requests

response = requests.get("https://gitlab.com/api/v4/users/ahmed_sh/projects")
print(response.status_code)  # 200
print(response.text)  # []
print(response.json()) # []

我正在尝试使用 python API 获取我的 GitLab 回购项目列表,但没有任何输出!虽然,当我使用浏览器时,我得到了一个非空的响应。我该如何解决这个问题?

这是因为您的用户命名空间中没有任何 public 项目。如果您想在您的命名空间中查看您的私人项目,您需要通过在 PRIVATE-TOKEN header.

中传递个人访问令牌来使用 API 进行身份验证

请注意,这也不会显示您在其他命名空间中处理的项目。

headers = {'PRIVATE-TOKEN': 'Your API key here!'}
resp = requests.get('https://gitlab.com/api/v4/users/ahmed_sh/projects', headers=headers)
print(resp.json())