【问题标题】:java api to get a file content for enterprise githubjava api获取企业github的文件内容
【发布时间】:2014-07-15 00:48:48
【问题描述】:

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

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

现在我正在尝试 egit:

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

GitHubRequest request = new GitHubRequest();

request.setUri("/readme");

GitHubResponse response = client.get(request);

然后呢?我只看到了一个getBody,也许我需要用某种json库来解析它?它必须更简单..我期待类似:repo.get(url).getContent()

【问题讨论】:

    标签: github github-api jcabi


    【解决方案1】:

    终于通过阅读源码搞清楚了..

        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();
        }
    

    【讨论】:

    • 非常感谢。正是我想要的
    • 这是一段非常有用的代码,以防有人想从 git 存储库中流式传输文件而不是获取内容。下面的代码可以做到这一点:List&lt;RepositoryContents&gt; contentList = contentService.getContents(repo); for(RepositoryContents content : test){ String fileConent = content.getContent(); InputStream is = new ByteArrayInputStream(Base64.decodeBase64(fileConent.getBytes())); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多