重定向被阻止的异常

时间:2018-05-14 19:27:51

标签: java maven

尝试使用github api创建远程存储库时,我收到重定向阻止异常。 我也有写权限推送到github。 任何人都可以帮忙吗?

以下是使用的代码:

 public static void main(String[] args) throws IOException, GitAPIException, URISyntaxException {
        // first create a test-repository, the return is including the .get directory here!
        File repoDir = createSampleGitRepo();

        // now open the resulting repository with a FileRepositoryBuilder
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        try (Repository repository = builder.setGitDir(repoDir)
                .readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build()) {
            System.out.println("Having repository: " + repository.getDirectory());

            // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
            Ref head = repository.exactRef("refs/heads/master");
            System.out.println("Ref of refs/heads/master: " + head);



            Git git = new Git(repository);
         // add remote repo:
            try{
                RemoteAddCommand remoteAddCommand = git.remoteAdd();
           //     System.out.println(remoteAddCommand);


               remoteAddCommand.setName("origin");
            remoteAddCommand.setUri(new URIish("URL"));
                // you can add more settings here if needed
                remoteAddCommand.call();

                git.commit().setMessage("try").call();
                // push to remote:
               PushCommand pushCommand = git.push();
                pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("user", "pwd"));
                // you can add more settings here if needed
                pushCommand.setRemote("https://github.com/org").call();
                git.push().setForce(true).call();

                git.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }




        // clean up here to not keep using more and more disk-space for these samples
      //  FileUtils.deleteDirectory(repoDir.getParentFile());
    }

    private static File createSampleGitRepo() throws IOException, GitAPIException, URISyntaxException {
        try (Repository repository = createNewRepository()) {
            System.out.println("Temporary repository at " + repository.getDirectory());

            // create the file
            File myFile = new File(repository.getDirectory().getParent(), "testfile");
            if(!myFile.createNewFile()) {
                throw new IOException("Could not create file " + myFile);
            }

            // run the add-call
            try (Git git = new Git(repository)) {

                URIish url = new URIish("https://github.com/org");

                //git.remoteSetUrl().setUri(url);

                PushCommand pushCommand = git.push();
                pushCommand
                        .setCredentialsProvider(new UsernamePasswordCredentialsProvider(
                                "user",
                                "pwd"));
                git.add()
                        .addFilepattern("testfile")
                        .call();


                // and then commit the changes
                git.commit()
                        .setMessage("Added testfile")
                        .call();
            }

            System.out.println("Added file " + myFile + " to repository at " + repository.getDirectory());

            return repository.getDirectory();
        }
    }
    public static Repository createNewRepository() throws IOException { 
        // prepare a new folder 
        String path = "Rep/";
        File localPath = new File(path, ".git"); 
        localPath.delete(); 

        // create the directory 
        Repository repository = FileRepositoryBuilder.create(new File(localPath, "")); 
        repository.create(); 

        return repository; 
    } 

}

例外:

org.eclipse.jgit.api.errors.TransportException: http://github.com/org/: Redirection blocked: redirect https://github.com/org/ -> https://github.com/login?return_to=https%3A%2F%2Fgithub.corp.ebay.com%2Fvtrmigrated%2Finfo%2Frefs%3Fservice%3Dgit-receive-pack not allowed

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题,你有没有解决这个问题

相关问题