我正在玩一些Javascript和Github API,我遇到了一个问题.
每当我尝试呼叫任何拥有粉丝的用户的关注者时,我从服务器获得的回调只显示30个用户.例如:
https://api.github.com/users/vojtajina/followers - 30粉丝
和原始网站的用户关注者:
https://github.com/vojtajina/followers - 1,039粉丝
我的问题是 - 发生了什么?来自服务器的回调中没有"下一页".如何在回调中获得他/她的所有粉丝?
每页的最大项目数为100,因此使用per_page=100querystring参数会使结果增加到每页100个用户:
https://api.github.com/users/vojtajina/followers?per_page=100
Run Code Online (Sandbox Code Playgroud)
使用pagequerystring参数,您可以控制分页.例如,要获取第二页,您应该添加page=2:
https://api.github.com/users/vojtajina/followers?per_page=100&page=2
Run Code Online (Sandbox Code Playgroud)
如果你想获得所有关注者,你必须迭代页面,直到你收到一个空数组.
如果你想将它用于Node.js/JavaScript(在客户端)应用程序,你可以使用gh.js-a我开发的库处理这个:
var GitHub = require("gh.js");
var gh = new GitHub({
token: "an optional token"
});
gh.get("users/vojtajina/followers", { all: true } function (err, followers) {
console.log(err || followers); // do something with the followers
});
Run Code Online (Sandbox Code Playgroud)