-3

Could someone help me how to connect to Github & how to upload a document using Java program ?

I want to connect to Github and once its connected I want to upload a document to github using Java program.

Many thanks, Raju

Raju
  • 1
  • 1
  • 1
  • 1
    https://github.com/eclipse/egit-github/tree/master/org.eclipse.egit.github.core – Christian Jan 16 '18 at 14:04
  • 1
    We need more details. If you want to connect with github you may need to use github api. Try to hit the follow url https://api.github.com/repos/vmg/redcarpet you should get some data back which is JSON. Use a framework like servlet or spring mvc to make calls to github api. Please read documentation and provide more information such as are you developing a web app/console application/desktop app ? – dijam Jan 16 '18 at 14:06
  • @djam I am not developing any app..My requirement is just to upload a file to GitHub using Java, I tried to do using Github API but still couldnt able to authenticate.. – Raju Jan 16 '18 at 14:37

1 Answers1

2

The simplest solution is to use one of the Github libraries. If you are interested in Java then you need to use this one.

First you need to authenticate using your Github account. Here is the sample from readme:

//Basic authentication
GitHubClient client = new GitHubClient();
client.setCredentials("user", "passw0rd");
//OAuth2 token authentication
GitHubClient client = new GitHubClient();
client.setOAuth2Token("SlAV32hkKG");

Then it depends on how you want to upload a document to Github. The simplest is to create a Gist. To do it use the following code:

GistFile file = new GistFile();
file.setContent("System.out.println(\"Hello World\");");
Gist gist = new Gist();
gist.setDescription("Prints a string to standard out");
gist.setFiles(Collections.singletonMap("Hello.java", file));
GistService service = new GistService();
service.getClient().setCredentials("user", "passw0rd");
gist = service.createGist(gist); //returns the created gist

Note Gists are publicly available so if you want to make it private you need to do it explicitly

  • Hi Anatolli, I couldnt able to find GitHubClient class in Github API .. – Raju Jan 16 '18 at 14:42
  • Hi. Here it is 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 you added library correctly. Do you use some build tool like Maven? – Anatolii Vakaliuk Jan 16 '18 at 14:55
  • Ya it's in the org.eclipse.egit.github library,not in GitHub API-- I am using Gradle and now if i want to check whether i could able to authenticate to Github how to do that-- I wrote the GitHubClient client = new GitHubClient(); client.setCredentials("user", "passw0rd"); But how to verify that the authentcaton is success ? Do i need to write any assert statement here ? could u pls help in detail pls.. – Raju Jan 16 '18 at 15:02
  • You need to wrap service.createGist(gist) call in try catch block. There are basically two exceptions you are interested in: IOException and RequestException. IOException means that there is some interaction error related to transfer. RequestException is defined in the library to indicate api interaction error. Check this https://github.com/eclipse/egit-github/blob/master/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/RequestException.java. Request exception has the status code inside. For authentication error it's usually 401 – Anatolii Vakaliuk Jan 16 '18 at 15:16