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

如何通过GitHub API v3获取用户全部公开仓库?分页请求异常排查

How to Fetch Paginated GitHub Repo Data Correctly

Hey there! Let's break down why your pagination request is failing and walk through how to fix it to get all of octocat's repos.

Common Issues with Your Request

First, let's address why https://api.github.com/users/octocat/repos?page=3&per_page=100 might not work:

  • Rate Limiting: Unauthenticated GitHub API requests are capped at 60 per hour. If you've made a bunch of requests already, you might hit a 403 Forbidden error. Authenticated requests get a much higher limit (5000 per hour), so this is often the quick fix.
  • Invalid Page Number: Octocat doesn't have 300+ public repos (last I checked, it's around 60). When you set per_page=100, page 1 will return all existing repos, and pages 2+ will return an empty array (which might feel like it's "not working" even though it's a valid response).
  • Missing Authentication Headers: Even for public repos, adding authentication helps avoid rate limits and ensures more reliable requests overall.

Step-by-Step Solution

  1. Check Total Repos & Valid Pages

    • First, call the base endpoint https://api.github.com/users/octocat/repos and look at the Link response header. It will look something like this:

      <https://api.github.com/users/octocat/repos?page=2&per_page=30>; rel="next", <https://api.github.com/users/octocat/repos?page=3&per_page=30>; rel="last"

    • The rel="last" link tells you the maximum valid page number when using the default per_page=30. Alternatively, call https://api.github.com/users/octocat and check the public_repos field to get the total count of repos directly.
  2. Use Authenticated Requests

    • Generate a GitHub Personal Access Token (PAT) — you don't need special permissions for public repos (even a token with no scopes will work to increase your rate limit).
    • Add the token to your request headers. For example, with curl:
      curl -H "Authorization: token YOUR_PERSONAL_ACCESS_TOKEN" "https://api.github.com/users/octocat/repos?page=2&per_page=100"
      
  3. Adjust Pagination Parameters

    • Since per_page maxes out at 100, calculate how many pages you need: divide the total repo count by 100 and round up. For example, if octocat has 60 repos, only page 1 will return data when using per_page=100.

Troubleshooting Tips

  • If you get a 403 Forbidden: You've hit the rate limit. Wait an hour, or switch to authenticated requests.
  • If you get an empty array: The page number you requested has no data (you've gone past the last page of repos).
  • If you get a 401 Unauthorized: Your PAT is invalid or expired. Generate a new one.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:21:31