java api获取企业github的文件内容

时间:2014-07-15 00:48:49

标签: github github-api jcabi

我努力寻找一个简单的代码行,它使用oauth令牌从企业github读取文件内容,但找不到这样的示例。

我试过https://github.com/jcabi/jcabi-github,但它不支持企业github?(也许我错了)

现在我正在尝试自我:

GitHubClient client = new GitHubClient("enterprise url");

GitHubRequest request = new GitHubRequest();

request.setUri("/readme");

GitHubResponse response = client.get(request);
那么什么?我只看到了一个getBody,也许我需要用一些json库来解析它?它必须更简单..我期待的东西像:repo.get(url).getContent()

1 个答案:

答案 0 :(得分:7)

最后通过阅读源代码来弄清楚..

    GitHubClient client = new GitHubClient(YOURENTERPRICEURL);
    client.setOAuth2Token(token);

    // first use token service
    RepositoryService repoService = new RepositoryService(client);

    try {
        Repository repo = repoService.getRepository(USER, REPONAME);

        // now contents service
        ContentsService contentService = new ContentsService(client);
        List<RepositoryContents> test = contentService.getContents(repo, YOURFILENAME);

        List<RepositoryContents> contentList = contentService.getContents(repo);
        for(RepositoryContents content : test){
            String fileConent = content.getContent();
            String valueDecoded= new String(Base64.decodeBase64(fileConent.getBytes() ));
            System.out.println(valueDecoded);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }