简体   繁体   中英

How can i connect to the github enterprise repository through java code?

I want to know how can i get the list of all the repositories present in my github enterprise(private). I am unable to identify how should i use my personal access token to get authentication through java code.

I have already tried with public repositories and i am able to use everything in that but i am unable to do this with my enterprise 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);

      }
}

Best way do do that in java without the burden of rest authent is to use one of the Java API available. This github page list all the APIs :

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

And you have 2 java intersting API:

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

and

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

egit-github seems quite easy to work with...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM