【问题标题】:how to create issue using bitbucket API and singpost in java如何在 java 中使用 bitbucket API 和 singpost 创建问题
【发布时间】:2013-03-04 00:11:38
【问题描述】:

我正在尝试在 bibucket 上创建新问题,但我不知道如何使用 http。我尝试了很多东西,但它仍然不起作用。这是我的尝试之一:

URL url = new URL("https://api.bitbucket.org/1.0/repositories/" 
        + accountname + "/" + repo_slug + "/issues/"
        + "?title=test&content=testtest");

HttpsURLConnection request = (HttpsURLConnection) url.openConnection();       
request.setRequestMethod("POST");
consumer.sign(request);
request.connect();

我对 GET 请求没有意见。但是在这里我不知道如何发送参数并签署消息。

这里是 API 的文档 https://confluence.atlassian.com/display/BITBUCKET/issues+Resource#issuesResource-POSTanewissue

如何正确地做到这一点?

【问题讨论】:

    标签: java signpost


    【解决方案1】:

    最后我想通了。参数不是 URL 的一部分,但如果您使用流,则无法对其进行签名。

    解决方案是使用 Apache HttpComponents 库并添加如下代码中的参数:

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("https://api.bitbucket.org/1.0/repositories/"
                + accountname + "/" + repo_slug + "/issues/");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("title", "test"));
        nvps.add(new BasicNameValuePair("content", "testtest"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        consumer.sign(httpPost); 
        HttpResponse response2 = httpclient.execute(httpPost);
    
        try {
            System.out.println(response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity2);
        } finally {
            httpPost.releaseConnection();
        }
    
    }
    

    但您必须使用 CommonsHttpOAuthConsumer,它位于 commonshttp 的特殊路标库中。

    【讨论】:

      【解决方案2】:

      我看到您已经解决了,但是here 它说您需要使用 OAuth 进行身份验证,并且在您链接的页面中,您需要进行身份验证才能创建新问题。 它还链接到此page,用于多种语言的 OAuth 实现。我将其发布为知识。

      【讨论】:

      • 确实如此。在我的代码中,我假设我已经通过路标进行了身份验证,并且我有 Consumer 的实例。谢谢
      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2019-08-24
      • 2016-11-16
      • 2017-09-23
      • 1970-01-01
      • 2018-08-08
      相关资源
      最近更新 更多