如何在java中调用github API表单HttpsURLConnection

时间:2017-01-16 06:27:56

标签: java github github-api httpsurlconnection

我需要将名为testingProtectedBranch1的分支更改为受保护的分支,并提供以下参数 在

  

required_status_check:include_admins = true,strict = true,context = continuous-integration / travis-ci

     

限制:null

     

required_pull_request_reviews:include_admins = false

这是我的代码,访问令牌(变量token)由用户在运行时提供。

public void setMasterBranchAsProtected() throws Exception{
   String URLForCallingTheBranchAPI="https://api.github.com/repos/kasunsiyambalapitiya/testingProtectedBranch1/branches/master/protection";

  String  jsonInput="{\"required_status_checks\":{\"include_admins\":true,\"strict\":true,\"contexts\":[\"continuous-integration/travis-ci\"]},"
           + "\"restrictions\":null,"
           + "\"required_pull_request_reviews\":{\"include_admins\":false} ";


    try {
        URL urlObject= new URL(URLForCallingTheBranchAPI);

        HttpsURLConnection httpsURLCon= (HttpsURLConnection)urlObject.openConnection();
        httpsURLCon.setDoOutput(true);
        httpsURLCon.setRequestMethod("PUT");
        httpsURLCon.setRequestProperty("User-Agent", "Mozilla/5.0");


        httpsURLCon.setRequestProperty("Accept","application/vnd.github.loki-preview+json");
        httpsURLCon.setRequestProperty("Authorization", "Bearer "+token);


        OutputStreamWriter outputStream= new OutputStreamWriter(httpsURLCon.getOutputStream());


        outputStream.write(jsonInput);

        int responseCode= httpsURLCon.getResponseCode();

        outputStream.flush();
        outputStream.close();






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



}

但对于响应代码,我收到类似于不可处理实体的422。我在这里做错了,请帮我解决这个问题。提前谢谢。

0 个答案:

没有答案