Github api 请求速率说明和请求限制说明,使用认证token增加请求次数

有能力的可以看官方文档:Resources in the REST API - GitHub Docs

 GitHub 对每小时可以发送的请求数量有限制。通常,GitHub API的标准限制为:

  • 未经身份验证 - 每个原始 IP 地址每小时60个请求;
  • 已验证 – 每个用户每小时可发送 5,000 个请求。

可以通过 https://api.github.com/users/octocat 查询是否限制了, 如下:

{
   "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
   "documentation_url": "https://developer.github.com/v3/#rate-limiting"
}

申请access token认证

进入github =>

点击头像 =>

点击 setting =>

Developer settings =>

Personal access tokens =>

点击右侧 Generate new token =>

在 Note 里添加备注,滚动页面到最底部,点击 Generate token 按钮

这时你就能看到新生成的 access token 啦!

那怎么验证请求次数是否真的增加了呢?

可以通过https://api.github.com/rate_limit?access_token=新生成的token 来验证

如图,limit已经增加到了5000次

curl --location 'https://api.github.com/rate_limit?access_token=你的token' \
--header 'Accept: application/vnd.github+json' \
--header 'Authorization: Bearer 你的token' \
--header 'X-GitHub-Api-Version: 2022-11-28'

如何使用

这里要注意 axios 添加 header 参数的格式:

// 查询模板仓库
Project.prototype.getTemplateFromRepo = async function () {
  const getTemplate = ora('正在获取模板,请稍等...')
  getTemplate.start();
  try {
    const res = await axios({ url: 'https://api.github.com/users/zonghua2016/repos', method: 'GET', headers: { "Authorization": `token${gitToken}` } })
    if (res.status === 200) {
      getTemplate.color = 'green';
      getTemplate.succeed('模板获取成功');
      return res.data.filter(repo => {
        if (repo.name.match(/aggna-(.*)-template/g)) {
          return repo
        }
      })
    }
  } catch (error) {
    getTemplate.color = 'red';
    getTemplate.fail(`模板获取失败:${error.response.statusText}`);
    return;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024小神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值