使用DotNetOpenAuth和Jira 5.1.2

时间:2013-03-11 17:09:22

标签: c# jira dotnetopenauth

我使用DotNetOpenAuth与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);
}

但是,webRequest.GetRequestStream()只会引发异常This property cannot be set after writing has started.

我正在尝试使用http://docs.atlassian.com/jira/REST/latest/#id120664创建新问题。如果我使用基本身份验证而不是OAuth,并且使用GET的所有其他OAuth调用工作正常,则代码可以正常工作。

任何人都有使用DotNetOpenAuth和Jira的建议吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

终于找到了问题。需要使用以下代码:

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

基本上,我需要在调用HttpDeliveryMethods.AuthorizationHeaderRequest时添加PrepareAuthorizedRequest,然后我需要在向流中添加任何内容之前设置ContentType属性。