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

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

How to connect to github using Java Program

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...