如何增加GitHub api的访问次数(从每小时60到每小时5000)

如何增加GitHub api的访问次数(从每小时60到每小时5000)

1. 如果没有认证

在没有认证的情况下,GitHub的api默认访问次数是每小时60,所以当你需要很多数据的时候,这点就完全不够用了。
怎么查看你的访问次数呢?官方文档给了这样的说明:https://developer.github.com/v3/
文档中的例子
说明手册的例子

curl -i https://api.github.com/users/octocat

这是没有认证时,我的访问次数,就是60,剩余次数59.
这是没有认证时,我的访问次数,就是60,剩余次数59.
所以在这种速率的限制下,就会出现这样的报错。

{
   "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"
}

2 怎么认证

GitHub 的文档里给了认证的几种方法,但我也是初学初用,只搞明白了这种 access token

1) 怎么申请access token
首先,你当然要注册一个GitHub的账号啦

	第二步,登录github.com
	![在这里插入图片描述](https://img-blog.csdnimg.cn/20200605200926793.png)

在最上面的一栏,点击最右边的你的小头像

第三步,找到setting,点击它!
在这里插入图片描述
第四步,在左侧的一栏找到developer setting
在这里插入图片描述
第5步,选择personal access tokens
在这里插入图片描述
第6步,就是选择generate new token
在这里插入图片描述
第七步,就是填写必要的信息,note我觉得就是给token起个名吧。我看很多人建议不要选任何复选框,可是我太欠了,我选了第一个,可是后来用的时候也没啥影响
在这里插入图片描述
最后你就可以拿到你的token了!
在这里插入图片描述
2) 怎么用access token呢
这个真的有点烦躁,我是没看到任何直接告诉我怎么用的答案,因为我是用request爬虫,所以curl -的认证方式不太适合我。
马上贴上我的代码片:

item="https://api.github.com/repos/BancCoin/banccoin-master"#这是我爬虫的项目网址
headers={"Authorization":"token "+"你的access token"}
r = requests.get(item,headers=headers)

就这样!简单!结束!搞定!
瞬间速率提到5000

这个网址可以查到你当前的访问速率,但是这种方式现在被淘汰了,具体要用什么方式呢,GitHub团队的邮件会告诉你,可是我得过且过没有研究,希望知道的大佬告诉我们一下!靴靴!没啦!搞定!
https://api.github.com/rate_limit?access_token=你的token

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个Java代码示例,使用GitHub APIGitHub仓库中读取文件内容: ```java import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Base64; public class GitHubFileReader { public static void main(String[] args) throws IOException { // GitHub API URL String apiUrl = "https://api.github.com/repos/{owner}/{repo}/contents/{path}"; // GitHub repository information String owner = "github"; String repo = "gitignore"; String path = "Java.gitignore"; // Replace placeholders with actual values apiUrl = apiUrl.replace("{owner}", owner) .replace("{repo}", repo) .replace("{path}", path); // Set up HTTP connection URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/vnd.github.v3+json"); // Set up authentication if needed String username = "username"; String token = "token"; String auth = username + ":" + token; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8)); String authHeaderValue = "Basic " + new String(encodedAuth); connection.setRequestProperty("Authorization", authHeaderValue); // Read response InputStream responseStream = connection.getInputStream(); String response = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8); // Extract content from response int start = response.indexOf("\"content\": \"") + 12; int end = response.indexOf("\",", start); String content = response.substring(start, end); // Decode content String decodedContent = new String(Base64.getDecoder().decode(content), StandardCharsets.UTF_8); // Print content System.out.println(decodedContent); } } ``` 这个代码示例假设你已经有了GitHub API访问令牌,用于身份验证。如果你没有访问令牌,你需要创建一个并将其传递到HTTP请求中,以便进行身份验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值