3

我在使用 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 有什么建议吗?

谢谢!

4

1 回答 1

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在向流中添加任何内容之前设置属性。

于 2013-03-12T14:45:40.120 回答