Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
563 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...