I'm trying to create an dot net app to fetch all the jira ticket present in a project, let say DevProject. I'm trying to use jira api but I'm either getting and empty response or 404 error. I'm sure I'm missing something i'm just not sure what.
This is my company's jira account to the baseurl the i'm using is "https://mycompany.atlassian.net" and restUrl is "/rest/api/3/issue/devissue"
here's a simple snippet of code
HttpWebResponse response = null;
HttpWebRequest request = WebRequest.Create(baseurl+restUrl) as HttpWebRequest;
request.Method = "GET";
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Basic " + GetEncodedCredentials("myUsename", "password"));
string responseContent;
using (response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
responseContent = reader.ReadToEnd();
}
In the password section I tried adding api token too but it didn't work.
I'm expecting it to return either issue or anything to check whether I'm calling the api in right way or i'm missing something. Any suggestion will be helpful.
I have created an API token too using my company's jira domain.
Here is a quick implementation based on their API docs.
async void Main()
{
string jiraBaseUrl = "YOUR_BASE_URL";
string issueKey = "YOUR_TICKET_KEY"; // Replace with your JIRA issue key
string email = "YOUR_EMAIL_TIED_TO_API_TOKEN";
string apiToken = "YOUR_API_TOKEN"; // Replace with your JIRA API token
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(jiraBaseUrl);
var base64Credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{email}:{apiToken}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
HttpResponseMessage response = await client.GetAsync($"/rest/api/3/issue/{issueKey}");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Root issue = JsonConvert.DeserializeObject<Root>(json);
//json.Dump();
issue.Dump();
issue.Fields.Content.Dump();
string formattedDescription = FormatDescription(issue.Content);
Console.WriteLine(formattedDescription);
//var issue = JsonSerializer.Deserialize<Issue>(json);
//Console.WriteLine($"Issue Key: {issue.Key}");
//Console.WriteLine($"Summary: {issue.Fields.Summary}");
//Console.WriteLine($"Description: {issue.Fields.Description}");
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
string errorMessage = await response.Content.ReadAsStringAsync();
Console.WriteLine(errorMessage);
}
}
}
public static string FormatDescription(List<Content> contentList)
{
if (contentList == null || contentList.Count == 0)
return string.Empty;
return string.Join("\n\n", contentList.SelectMany(content => content.Contents.Select(ParseContentItem)));
}
public static string ParseContent(Content content)
{
if (content == null || content.Contents == null)
return string.Empty;
return string.Join("", content.Contents.Select(ParseContentItem));
}
public static string ParseContentItem(ContentItem contentItem)
{
if (contentItem == null)
return string.Empty;
string text = contentItem.Text ?? "";
if (contentItem.Attrs != null && !string.IsNullOrEmpty(contentItem.Attrs.Text))
{
text = contentItem.Attrs.Text;
}
return text;
}
public class AvatarUrls
{
[JsonProperty("48x48")]
public string _48x48 { get; set; }
[JsonProperty("24x24")]
public string _24x24 { get; set; }
[JsonProperty("16x16")]
public string _16x16 { get; set; }
[JsonProperty("32x32")]
public string _32x32 { get; set; }
}
public class AccountType
{
public string AccountId { get; set; }
public string EmailAddress { get; set; }
public AvatarUrls AvatarUrls { get; set; }
public string DisplayName { get; set; }
public bool Active { get; set; }
public string TimeZone { get; set; }
public string AccountTypes { get; set; }
}
public class Customfield10993
{
public string Type { get; set; }
public int Version { get; set; }
public List<Content> Content { get; set; }
}
public class Content
{
public string Type { get; set; }
public List<ContentItem> Contents { get; set; }
}
public class ContentItem
{
public string Type { get; set; }
public string Text { get; set; }
public MentionAttrs Attrs { get; set; }
public List<Mark> Marks { get; set; }
}
public class MentionAttrs
{
public string Id { get; set; }
public string Text { get; set; }
public string AccessLevel { get; set; }
}
public class Mark
{
public string Type { get; set; }
public MarkAttrs Attrs { get; set; }
}
public class MarkAttrs
{
public string Href { get; set; }
}
public class Issuelinks
{
public string Id { get; set; }
public string Self { get; set; }
public LinkType Type { get; set; }
public InwardIssue InwardIssue { get; set; }
}
public class LinkType
{
public string Id { get; set; }
public string Name { get; set; }
public string Inward { get; set; }
public string Outward { get; set; }
public string Self { get; set; }
}
public class InwardIssue
{
public string Id { get; set; }
public string Key { get; set; }
public string Self { get; set; }
public Fields Fields { get; set; }
}
public class Fields
{
public string Summary { get; set; }
public Status Status { get; set; }
public Priority Priority { get; set; }
public Issuetype Issuetype { get; set; }
public AccountType Assignee { get; set; }
public List<Content> Content { get; set; }
}
public class Status
{
public string Self { get; set; }
public string Description { get; set; }
public string IconUrl { get; set; }
public string Name { get; set; }
public string Id { get; set; }
public StatusCategory StatusCategory { get; set; }
}
public class StatusCategory
{
public string Self { get; set; }
public int Id { get; set; }
public string Key { get; set; }
public string ColorName { get; set; }
public string Name { get; set; }
}
public class Priority
{
public string Self { get; set; }
public string IconUrl { get; set; }
public string Name { get; set; }
public string Id { get; set; }
}
public class Issuetype
{
public string Self { get; set; }
public string Id { get; set; }
public string Description { get; set; }
public string IconUrl { get; set; }
public string Name { get; set; }
public bool Subtask { get; set; }
public int AvatarId { get; set; }
public int HierarchyLevel { get; set; }
}
public class Comment
{
public string Self { get; set; }
public string Id { get; set; }
public AccountType Author { get; set; }
public Body Body { get; set; }
public AccountType UpdateAuthor { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public bool JsdPublic { get; set; }
}
public class Body
{
public int Version { get; set; }
public string Type { get; set; }
public List<Content> Content { get; set; }
}
public class CommentContainer
{
public List<Comment> Comments { get; set; }
public string Self { get; set; }
public int MaxResults { get; set; }
public int Total { get; set; }
public int StartAt { get; set; }
}
public class Root
{
public string Expand { get; set; }
public string Id { get; set; }
public string Self { get; set; }
public string Key { get; set; }
public Fields Fields { get; set; }
public List<Content> Content { get; set; } // Add this property
}