You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Gitlab API和python-gitlab获取用户的全部星标项目

实现方案

python-gitlab未在User对象上封装星标项目的快捷调用方法,你可以直接调用Gitlab原生API接口实现需求,可用代码如下:

import gitlab

# 初始化Gitlab客户端,私有令牌需在Gitlab个人设置的访问令牌页面生成,需授予read_api权限
# 如果是自建Gitlab,将url替换为你的自建实例地址
gl = gitlab.Gitlab(url="https://gitlab.com", private_token="你的私有访问令牌")

# 场景1:获取当前登录账号的星标项目
my_starred = gl.user_starred_projects.list(all=True)
for project in my_starred:
    print(f"项目名:{project.name}  访问地址:{project.web_url}")

# 场景2:获取指定公开用户的星标项目
target_username = "目标用户的用户名"
user = gl.users.list(username=target_username)[0]
# 调用原生接口拉取星标项目
user_starred = gl.http_get(f"/users/{user.id}/starred_projects", per_page=100)
for project in user_starred:
    print(f"项目名:{project['name']}  访问地址:{project['web_url']}")

说明

  • 你之前代码报错的原因是python-gitlab没有封装get_starred()、starred()这类User对象的快捷方法,所以直接调用会触发属性错误
  • 如果要拉取其他用户的星标项目,需要该用户的星标列表权限为公开,否则只能拉取你有权限可见的部分项目
  • 接口默认分页返回结果,若星标项目超过100个,需要循环拉取所有分页的内容

内容的提问来源于stack exchange,提问作者NoDataDumpNoContribution

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:45:03