Hello I am trying to connect to the JIRA Rest API to obtain the JSON Data for further parsing. I am using BASIC Auth + API Token generated with my company login at the Atlassian site : https://id.atlassian.com/manage-profile/security/api-tokens
I get an error 401 : Unauthorized. What I am doing wrong. I am new to .NET and would really help if experts in our community help fix the code or let me know what I am doing wrong. I am expecting JSON Data from the URL for a given component under a project. I am admin on that project and can see the JSON data when opening the URL directly in a browser.
With the WSSO redirection it gives the error NO redirection URL found.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using Newtonsoft.Json.Linq;
namespace JIRA_Data_Pull_Integration
{
class Class3
{
private static void Main()
{
Console.WriteLine("Started");
RunQuery("");
Console.WriteLine("Finished");
}
private static string RunQuery(string query, string argument = null, string data = null, string method = "GET")
{
try
{
HttpWebRequest newRequest = WebRequest.Create("https://wsso-support.web.boeing.com:2016/redirect.html?URL=https://jira-ent.web.boeing.com/rest/api/2/search?jql=project=ECS_CORE%20AND%20Component=BIGROCKS") as HttpWebRequest;
newRequest.ContentType = "application/json";
newRequest.Accept = "application/json";
newRequest.Method = "GET";
newRequest.AllowAutoRedirect = true;
string base64Credentials = GetEncodedCredentials();
newRequest.Headers.Add("Authorization", "Basic" + base64Credentials);
HttpWebResponse response = newRequest.GetResponse() as HttpWebResponse;
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
newRequest = null;
response = null;
return result;
}
catch (Exception)
{
throw;
}
}
private static string GetEncodedCredentials()
{
string mergedCredentials = string.Format("{0}:{1}","anshuman.samal@boeing.com", "XXXXXXXXXXXX");
Console.WriteLine(mergedCredentials);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
}
}