java - 使用 org.kohsuke.github.GHUserSearchBuilder 时出现 API 速率限制错误

标签 java github authorization github-api rate-limiting

我的方法看起来是这样:

static void search() throws IOException {

    GitHub gitHub = GitHubBuilder
            .fromEnvironment()
            .withPassword(MyAuthData.LOGIN,MyAuthData.PASSWORD)
            .withRateLimitHandler(RateLimitHandler.WAIT)
            .build();

    GHUserSearchBuilder searchBuilder =   gitHub.searchUsers().language("Russian").location("Moscow");
    List<GHUser> users = searchBuilder.list().asList();
    for(GHUser user: users){
        System.out.println(user.getName()+" "+user.getEmail()+" "+user.getCompany());}
    System.out.println("Total: "+searchBuilder.list().getTotalCount());
}

这是我的日志:

Exception in thread "main" java.lang.Error: java.io.IOException: {"message":"API rate limit exceeded for alexey-semenov.","documentation_url":"https://developer.github.com/v3/#rate-limiting"}
    at org.kohsuke.github.Requester$1.fetch(Requester.java:399)
    at org.kohsuke.github.Requester$1.hasNext(Requester.java:366)
    at org.kohsuke.github.PagedSearchIterable$1.hasNext(PagedSearchIterable.java:49)
    at org.kohsuke.github.PagedIterator.fetch(PagedIterator.java:44)
    at org.kohsuke.github.PagedIterator.hasNext(PagedIterator.java:32)
    at org.kohsuke.github.PagedIterable.asList(PagedIterable.java:21)
    at com.company.Searcher.search(Searcher.java:34)
    at com.company.Main.main(Main.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.io.IOException: {"message":"API rate limit exceeded for alexey-semenov.","documentation_url":"httpss://developer.github.com/v3/#rate-limiting"}
    at org.kohsuke.github.Requester.handleApiError(Requester.java:506)
    at org.kohsuke.github.Requester$1.fetch(Requester.java:395)
    ... 12 more
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.github.com/search/users?q=language%3ARussian+location%3AMoscow&page=31
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1890)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1885)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at org.kohsuke.github.Requester.parse(Requester.java:461)
    at org.kohsuke.github.Requester.access$300(Requester.java:64)
    at org.kohsuke.github.Requester$1.fetch(Requester.java:390)
    ... 12 more
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: httpss://api.github.com/search/users?q=language%3ARussian+location%3AMoscow&page=31
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at org.kohsuke.github.Requester.parse(Requester.java:457)
    ... 14 more

谁遇到过类似的事情?这里如何进行正确授权?

最佳答案

您的身份验证正确 - 请注意 API 回复了以下消息:

API rate limit exceeded for alexey-semenov

这意味着 authentication成功了(因为您的帐户已被识别),但您 stepped over the rate limit即 5000 个请求/小时(每个用户)。如果您没有正确进行身份验证,则消息将如下所示:

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.)

关于java - 使用 org.kohsuke.github.GHUserSearchBuilder 时出现 API 速率限制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33566665/

相关文章:

git - 如何使用 git log 只获取带路径的文件名?

java - Spring Security 阻止所有请求

java - 以适当的顺序将项目添加到数组中

java - 如何从具有多个属性的类中获取一个特定属性?

java - 在 Clojure 中为 Java 返回值使用类型提示

Java android SensorEventListener 中的 OutOfMemoryError

javascript - 使用 ajax 编辑要点

python - 如何与开源项目共享我的环境变量?

java - Keycloak公共(public)客户端和授权

c# - 通过 HttpClient 使用 Google Cloud AutoML 时收到 401 "Unauthorized"错误