Connessione a GitHub in Java
GitHub API è una libreria per Java che ci consente di connetterci al nostro profilo di GitHub.
Attraverso la libreria possiamo eseguire praticamente tutte le operazioni sui repo, anche aggiungerli e cancellarli.
In questo articolo vediamo come usarla, ma non esploreremo tutte le funzioni.
Prima di tutto dovete creare un token di accesso nel vostro profilo.
Poi se usate Maven:
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.326</version>
</dependency>
Infine il codice:
package org.example;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.IOException;
import java.util.Map;
public class Main {
public static void main(String[] args) {
try {
String token = "TOKEN";
GitHub github = GitHub.connectUsingOAuth(token);
GHMyself user = github.getMyself();
System.out.println("USERNAME: " + user.getLogin());
System.out.println("NOME: " + user.getName());
System.out.println("EMAIL: " + user.getEmail());
System.out.println("==========");
System.out.println("LISTA REPO:");
Map<String, GHRepository> repos = github.getMyself().getRepositories();
for (GHRepository repo : repos.values()) {
System.out.println("NOME: " + repo.getName() + ", PRIVATO: " + repo.isPrivate());
}
System.out.println("==========");
System.out.println("DETTAGLIO REPO:");
GHRepository repo = github.getRepository(github.getMyself().getLogin() + "/horus-db");
System.out.println("NOME: " + repo.getName());
System.out.println("DESCRIZIONE: " + repo.getDescription());
System.out.println("URL: " + repo.getHtmlUrl());
System.out.println("STARS: " + repo.getStargazersCount());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Enjoy!
java maven github
Commentami!