首页 > 解决方案 > 如何通过 java 代码连接到 github 企业存储库?

问题描述

我想知道如何获取我的 github 企业(私有)中存在的所有存储库的列表。我无法确定我应该如何使用我的个人访问令牌通过 java 代码进行身份验证。

我已经尝试过使用公共存储库,并且我可以使用其中的所有内容,但我无法使用我的企业 github 执行此操作。

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.apache.commons.codec.binary.Base64;

public class httpget {

    public static void main(String args[]) throws IOException, ParseException,JSONException
    {
        URL url=new URL("https://github---.com/api/v3/...");
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        String token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String authString="Basic"+Base64.encodeBase64(token.getBytes());
        conn.setRequestProperty("Authorization", authString);
        conn.connect();
        String inline="";

        Scanner sc = new Scanner(url.openStream());
        while(sc.hasNext())
        {
            inline+=sc.nextLine();
        }
        sc.close();
        System.out.println(inline);

      }
}

标签: javagithub

解决方案


在 java 中做到这一点而不需要休息 authent 的最好方法是使用可用的 Java API 之一。这个 github 页面列出了所有的 API:

https://developer.github.com/v3/libraries/

你有 2 个 java 有趣的 API:

egit-github:https ://github.com/eclipse/egit-github/tree/master/org.eclipse.egit.github.core

kohsuke:http: //github-api.kohsuke.org/

egit-github 似乎很容易使用......


推荐阅读