如何通过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
Check Total Repos & Valid Pages
- First, call the base endpoint
https://api.github.com/users/octocat/reposand look at theLinkresponse 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 defaultper_page=30. Alternatively, callhttps://api.github.com/users/octocatand check thepublic_reposfield to get the total count of repos directly.
- First, call the base endpoint
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"
Adjust Pagination Parameters
- Since
per_pagemaxes 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 usingper_page=100.
- Since
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
相关产品推荐
相关产品推荐

