質問

I am trying to create new issue at bibucket, but i don't know how to work with http. I try many things but it stil dosen't work. This is one of my attempts:

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

I don't have a problem with GET requests. But here I don't know how to send parametrs and sign the message.

Here is documentation of API https://confluence.atlassian.com/display/BITBUCKET/issues+Resource#issuesResource-POSTanewissue

How to do this properly?

役に立ちましたか?

解決

In the end I figured this out. The parametrs isn't part of the URL, but if you use stream, You can't sign it.

The solution is use Apache HttpComponents library and add parameters like in code below:

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

}

But you must use CommonsHttpOAuthConsumer which is in special signpost library for commonshttp.

他のヒント

I have seen you've already solved but here it says that you need to authenticate with OAuth and in the page you linked that you need to authenticate in order to create new issues. It also links to this page for OAuth implementations for many languages. I am going to post it for knowledge.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top