Terry
Terry

Reputation: 895

Using DotNetOpenAuth with Jira 5.1.2

I've run into an issue using DotNetOpenAuth to communicate with Jira.

var payload =   
    new {
        fields = new
        {
            project = new { id = 10000 },
            summary = summary,
            description = description,
            issuetype = new { id = (int)issueTypeId }
        }
    };

webRequest = OAuthConsumer.PrepareAuthorizedRequest(
    new MessageReceivingEndpoint(url, HttpDeliveryMethods.PostRequest),
    accessToken
);

byte[] payloadContent = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(payload));
webRequest.ContentLength = payloadContent.Length;
using (var stream = webRequest.GetRequestStream())
{
    stream.Write(payloadContent, 0, payloadContent.Length);
}

However, webRequest.GetRequestStream() just throws an exception This property cannot be set after writing has started.

I'm attempting to create a new issue using http://docs.atlassian.com/jira/REST/latest/#id120664. The code works fine if I use basic authentication rather than OAuth and all my other OAuth calls using GET work just fine.

Anyone have any advice using DotNetOpenAuth with Jira?

Thanks!

Upvotes: 3

Views: 1397

Answers (1)

Terry
Terry

Reputation: 895

Finally found the issue. Needed to use the following code:

var payload =   
    new {
        fields = new
        {
            project = new { id = 10000 },
            summary = summary,
            description = description,
            issuetype = new { id = (int)issueTypeId }
        }
    };

webRequest = OAuthConsumer.PrepareAuthorizedRequest(
    new MessageReceivingEndpoint(url, HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest),
    accessToken
);

webRequest.ContentType = "application/json";

byte[] payloadContent = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(payload));
webRequest.ContentLength = payloadContent.Length;
using (var stream = webRequest.GetRequestStream())
{
    stream.Write(payloadContent, 0, payloadContent.Length);
}

Basically, I needed to add HttpDeliveryMethods.AuthorizationHeaderRequest when calling PrepareAuthorizedRequest and then I needed to set the ContentType property BEFORE adding anything to the stream.

Upvotes: 3

Related Questions