Using Jira's REST API

Tristan.Uglow November 19, 2019

I thought this was going to be simple, but it has proved surprisingly difficult.

What I want to achieve is to retrieve Jira Stories from the REST API using either jquery or C# (Visual Studio 2010, .NET 4).

The back-story is that we have a wall board (VDU displaying a web site) that is meant to show how well we're processing the Jira stories through the QA Department. To start with, I'll settle for extracting a "number of stories in that are in the QA column in the Kanban board for the current sprint". I'm can write the JQL query for this, but after several hours of trying, I've had no success getting data out of the Jira REST.

The seriously frustrating part is that if I go to the following URL in Chrome I get a screen-full of JSON that looks like exactly what I want (obviously, these are not my actual credentials, and yes, I had previously logged into Jira, so there is probably an authentication cookie set somewhere).

Does anybody have any suggestions? Much appreciated at this stage...

 

https://metricell.atlassian.net/rest/api/latest/search?username=email.address@company.com&password=myFakePassword&jql=issuetype=bug

2 answers

1 accepted

1 vote
Answer accepted
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 19, 2019

Hi @Tristan.Uglow 

You're correct that running the API via a browser uses your Jira login cookies.

If you're on the cloud-based Jira (which the tags suggest) then you MUST use the API token, password won't work. You then need to use the email address that was used to generate the token.

You obviously haven't included all the code - what value does the variable authType have? It needs to be Basic.

If you still don't get anywhere, you can try using Postman (either the online one or download the app), which handles the authorisation for you.

You could also try using the C# code I posted here - just use your e-mail address for the variable m_Username and the API token for the variable m_Password. (This code was posted 2 years ago prior to the authorisation changes)

Tristan.Uglow November 19, 2019

Thanks Warren. Sorry for missing authType. Actually, it was set to an empty string! I changed it to "Basic", and used the API Token. This has made a HUGE difference. I think I'm able to move forward now. Thanks to both you and @Hana Kučerová .

Like Rich Mertz likes this
0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 19, 2019

Hi @Tristan.Uglow,

which method do you use for authentication against the Jira REST API? Is it basic auth?

How does the response to you requests looks like? Is it possible to share it? Thank you.

I just want to bude sure, that you are able to get any data using REST API, than the next step is to get the right one :-).

Tristan.Uglow November 19, 2019

Hi Hana. Yes, I am trying to use basic auth. What I think I've done is to supply my email address and password both in the URL and also adding them to the headers as an encoded "username:password" string. I also created an API token, and tried using that as well.

I tried lots of different things. The best result I got so far was a "success" response from the API, but the only data that came back was an array of two bytes. I didn't think to check what they were (could have been 'O' and 'K' I guess!)

I'll get my code back to a point where it is returning something and share what I get with you. Thanks for your help!

Tristan.Uglow November 19, 2019

Here's an example of the VisualStudio 2010 C# code I'm using. When I run it I get a 401 error on the request.GetResponse() line. Please note - these are not the genuine URLs, usernames, or passwords.

 

            string strResponseValue = string.Empty;
 
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://company.atlassian.net/rest/api/latest/project?username=email.address@company.com&password=MyFakePassword");
 
            request.Method = httpMethod.ToString();
 
            string authHeaer = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("email.address@company.com" + ":" + "MyFakePassword"));
            request.Headers.Add("Authorization", authType.ToString() + " " + authHeaer);
 
            HttpWebResponse response = null;
 
            try 
            {
                response = (HttpWebResponse)request.GetResponse();
 
                using (Stream responseStream = response.GetResponseStream())
                {
                    if(responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            strResponseValue = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                strResponseValue = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
            }

 

Tristan.Uglow November 19, 2019

... and this is the kind of thing I get if I paste the same URL into Chrome. Again, I've changed some of the details, for security reasons.

 

[{"expand":"description,lead,issueTypes,url,projectKeys,permissions","self":"https://company.atlassian.net/rest/api/2/project/10016","id":"10016","key":"SB2","name":"ProductA v2","avatarUrls":{"48x48":"https://company.atlassian.net/secure/projectavatar?pid=10016&avatarId=10329","24x24":"https://company.atlassian.net/secure/projectavatar?size=small&s=small&pid=10016&avatarId=10329","16x16":"https://company.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10016&avatarId=10329","32x32":"https://company.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10016&avatarId=10329"},"projectTypeKey":"software","simplified":false,"style":"classic","isPrivate":false,"properties":{}},
{"expand":"description,lead,issueTypes,url,projectKeys,permissions","self":"https://company.atlassian.net/rest/api/2/project/10017","id":"10017","key":"SN2","name":"ProductB v2","avatarUrls":{"48x48":"https://company.atlassian.net/secure/projectavatar?pid=10017&avatarId=10200","24x24":"https://company.atlassian.net/secure/projectavatar?size=small&s=small&pid=10017&avatarId=10200","16x16":"https://company.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10017&avatarId=10200","32x32":"https://company.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10017&avatarId=10200"},"projectTypeKey":"software","simplified":false,"style":"classic","isPrivate":false,"properties":{}}
]

Suggest an answer

Log in or Sign up to answer