如何使用Java程序连接到github

如何使用Java程序连接到github

本文介绍了如何使用Java程序连接到github的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何连接到Github&吗?如何使用Java程序上传文档?

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

我想连接到Github,一旦连接好,我想使用Java程序将文档上传到github.

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

非常感谢,拉朱

推荐答案

最简单的解决方案是使用 Github 库之一.如果您对Java感兴趣,则需要使用这个.

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

首先,您需要使用您的 Github 帐户进行身份验证.这是自述文件中的示例:

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");

然后,这取决于您要如何将文档上传到 Github .最简单的方法是创建要点.为此,请使用以下代码:

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

这篇关于如何使用Java程序连接到github的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:10