0

I'm working with the github java api and i want to take info about repositories and their users. My question is how can i authorize my requests in order to have full access to the api (5000 requests/hour). Also it would be very hepful if there is way to see any time how many requests are remaining for my application in order not το outreach the api rate limit. The code below is what i do now but with this code i outreach the rate limit.

    this.username = ConfigurationParser.parse("username");
    this.password  = ConfigurationParser.parse("password");
    OAuthService oauthService = new OAuthService();
    oauthService.getClient().setCredentials(this.username, this.password);
    Authorization auth = new Authorization();
    try {
        auth = oauthService.createAuthorization(auth);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(auth.getApp().getName());

    service.getClient().setOAuth2Token(auth.getToken());
fchatzia
  • 45
  • 8

2 Answers2

0

The OAuthService class is for altering a user's OAuth authorizations, not for creating an authorized connection to GitHub! You won't need this, if you just want to grab info about repositories.

You should first decide if you want to use basic auth (the one with username and password) or OAuth authentication (using a token), then you instantiate a service object from one of the available service classes, depending on the info you want to retrieve.

For repository information, this would be:

RepositoryService service = new RepositoryService();

Then you add your authorization info:

service.getClient().setCredentials("user", "passw0rd");

OR

service.getClient().setOAuth2Token("your_t0ken");

Now you can query a list of the repositories or do anything else:

List<Repository> repositories = service.getRepositories();

To get the remaining requests, you can call:

service.getClient().getRemainingRequests();

which will return the number included in the latest response you got.

Jan Gerlinger
  • 7,361
  • 1
  • 44
  • 52
  • Thank you very much Jan, but method getRemainingRequests doesn't exist in the GithubClient class. What am i missing? – fchatzia Jan 17 '13 at 17:39
  • It is [definitely there](https://github.com/eclipse/egit-github/blob/master/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java). Please make sure that you are using the most recent version. – Jan Gerlinger Jan 18 '13 at 19:04
0

You can use jcabi-github (I'm one of its developers), which does all the authentication work for you:

Github github = new RtGithub("user", "password");

Now you have a client that can help you to manipulate Github entities, for example you can list all issues in a given repository:

Coordinates coords = new Coordinates.Simple("jcabi/jcabi-github");
Repo repo = github.repos().get(coords);
for (Issue issue : repo.issues().iterate(Collections.<String, String>emptyMap())) {
  System.out.println("Issue found: #" + issue.number());
}
yegor256
  • 102,010
  • 123
  • 446
  • 597