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);
}
}
}
To retrieve query results in JSON format from a JIRA Cloud instance using a valid API token in a .NET script, you can use the HttpClient class to make HTTP requests to the JIRA REST API. Here's a simple script to get started (not tested):
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Replace with your JIRA Cloud instance URL and API token
string jiraBaseUrl = "https://your-instance-name.atlassian.net";
string apiToken = "YOUR_API_TOKEN";
// JQL query to retrieve issues
string jqlQuery = "project = YOUR_PROJECT";
// Create an HttpClient instance
using (HttpClient client = new HttpClient())
{
// Set the base URL for the JIRA REST API
client.BaseAddress = new Uri($"{jiraBaseUrl}/rest/api/3/");
client.DefaultRequestHeaders.Accept.Clear();
// Set the authorization header using the API token
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"API_TOKEN:{apiToken}")));
// Specify the media type for JSON
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// Send a GET request to retrieve the query results
HttpResponseMessage response = await client.GetAsync($"search?jql={Uri.EscapeDataString(jqlQuery)}");
// Check if the request was successful
if (response.IsSuccessStatusCode)
{
// Read the response content as JSON
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
}
Be sure to replace "https://your-instance-name.atlassian.net" with your actual JIRA Cloud instance URL and "YOUR_API_TOKEN" with your valid API token. You can also customize the JQL query in the jqlQuery variable to specify the criteria for retrieving issues.
This script sends a GET request to the JIRA REST API, retrieves the query results, and prints the JSON response to the console. You can then parse and use the JSON data as needed for your application.
Thanks. I had this piece of code but it fails for all Output Type
A. for Out put type Windows and console both gives : Error "Program does not contain a statuS 'Main' method suitable for an entry point. Looks like the Main definition is not being accepted and it is running on .NET Framework 4.7
B. Class Library : gives :Error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did say it was untested.
The basic steps to run a query via the cloud API are in there, you will need to write your own script or troubleshoot my pseudocode
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My code wouldn't work since you had a Cloud token, I gave you an example for the Cloud.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The token you have seems to be for Cloud JIRA Instance, you seems to use a Server or Data Center of JIRA. You should use your password instead of the token.
You may also be able to create a personal access token depending of the version of JIRA you are using.
https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html
Regadrs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Florian,
Thank you so much for your quick response. I did try using my own password and ran into the same issue. Atlassian has deprecated the use of password
I now created a PAT Token and used instead of a API Token and it still gives the same error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you using a Cloud instance or A Server/Data Center instance ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Florian,
Also it will be great help if you can share any details/documents, that a Data Center JIRA installation admin must set up in order to allow REST API. Just want to get that checked, once I share that with Boeing DC JIRA Admin team so that they can cross check if nothing is preventing from accessing it through code.
Thanks
Anshuman Samal
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
There is no configuration I will think of. You have to make sure to check the appropriate documentation depending on the platform you use, Cloud or DC.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.