简体   繁体   English

C#JIRA工作日志更新错误“远程服务器返回错误:(401)未经授权。”

[英]C# JIRA work-log update error “The remote server returned an error: (401) Unauthorized.”

Im going to update work log of an issue in JIRA via the JIRA REST API on c# application. 我将通过c#应用程序上的JIRA REST API更新JIRA中问题的工作日志。 Following codes shows what I have done so far. 以下代码显示了我到目前为止所做的事情。

HttpWebResponse return this error "The remote server returned an error: (401) Unauthorized." HttpWebResponse返回此错误“远程服务器返回错误:(401)未经授权。” .

I tried this with same credentials and using same data in PHP cURL functions, it works fine and issue work log updated successfully. 我使用相同的凭据尝试了此操作,并在PHP cURL函数中使用了相同的数据,它可以正常工作并成功更新了工作日志。

This is my Jason converted serialized object : {"update":{"worklog":[{"add":{"comment":"Sample test comment by IJ","timeSpent":"210"}}]}} 这是我的Jason转换后的序列化对象: {“ update”:{“ worklog”:[{“ add”:{“ comment”:“ IJ的示例测试注释”,“ timeSpent”:“ 210”}}]}}}

protected string RunQuery(JiraResource resource, string argument = null, string data = null, string method = "PUT")
{
// Where;
// resource = issue
// argument = "JIRA-16"
// Data = {"update":{"worklog":[{"add":{"comment":"Sample test comment by IJ","timeSpent":"210"}}]}}
// Method = "PUT"

        string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());

        if (argument != null)
        {
            url = string.Format("{0}{1}", url, argument);
        }

// URL = https://companyname.atlassian.net/rest/api/2/issue/JIRA-16

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.ContentType = "application/json";
        request.Method = method;
        request.ContentLength = data.Length;

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write(data);
        }

        string base64Credentials = GetEncodedCredentials(); // check below
        request.Headers.Add("Authorization", "Basic " + base64Credentials);

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;// here returns the error
//The remote server returned an error: (401) Unauthorized.

        string result = string.Empty;
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
        }

        return result;
}

private string GetEncodedCredentials()
{
        string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        return Convert.ToBase64String(byteCredentials);
}

Where am I doing wrong? 我在哪里做错了? please help me. 请帮我。

Likely reason: you are adding authorization header after sending request stream. 可能的原因:您发送请求流添加了授权标头。

   var request = WebRequest.Create(url) as HttpWebRequest;
   request.ContentType = "application/json";
   request.Method = method;
   request.ContentLength = data.Length;

   // All headers MUST be added before writing to request stream
   request.Headers.Add("Authorization", "Basic " + GetEncodedCredentials());

   using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
   {
       writer.Write(data);
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程服务器返回错误:(401)未经授权。 wcf httpbinding基本 - The remote server returned an error: (401) Unauthorized. wcf httpbinding basic 远程服务器返回错误:(401) Unauthorized。 Twitter oAuth - The remote server returned an error: (401) Unauthorized. Twitter oAuth 出现错误:远程服务器返回错误:(401)未经授权。 ,同时将我的网站评论发布到Twitter。 使用C# - Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C# C# 远程服务器返回错误:(401) Unauthorized - C# The remote server returned an error: (401) Unauthorized System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() SharePoint 仅应用程序身份验证抛出“远程服务器返回错误:(401)未经授权。” - SharePoint App-Only Authentication throwing “The remote server returned an error: (401) Unauthorized.” 远程服务器返回错误:(401) 未经授权。 在 Visa 开发支付网关 - The remote server returned an error: (401) Unauthorized. In Visa develope payment Gateway 为什么我收到“远程服务器返回错误:(401) 未经授权”。 从这个代码片段? - Why am I getting 'The remote server returned an error: (401) Unauthorized.' from this code snippet? 错误远程服务器返回错误:(401)未经授权 - Error The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM