Connecting to JIRA via C# rest api

sean o'brien July 14, 2017

I'm using the Atlassian SDK from the following location: https://bitbucket.org/farmas/atlassian.net-sdk 

 

Right now, I'm simply trying to connect to my JIRA and just bring down some basic information like my tasks. Doing a quick google I found the following example: https://www.codeproject.com/Tips/762516/Connecting-to-Jira-using-Csharp

In the above link, he connects to JIRA using the following line:

Jira jiraConn = new Jira("http://yourjiraurl.com/", jUserID, jPassword);

But when I try the same line:

 Jira jira = new Jira(url, "admin", "password");

I get the following errors: 

cannot convert from 'string' to 'Atlassian.Jira.ServiceLocator'

cannot convert from 'string' to 'Atlassian.Jira.JiraCredentials'

cannot convert from 'string' to 'Atlassian.Jira.JiraCache'

I've looked around to try and find some documentation on this, but I can't find anything. Does anyone have any experience on combating these errors?

4 answers

1 accepted

4 votes
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.
July 14, 2017

Hi Sean

You need to install the nuget package mentioned in the link, which should then give you access to the Jira object.

Alternatively, I use the code below which works well. RunQuery is called with only the first parameter passed in, the others are used for Posting data back to Jira. It calls GetEncodedCredentials which requires your password and username (it was username until recently, now it requires your e-mail address, although I haven't renamed the variable). The query passed in to RunQuery is obtained from https://docs.atlassian.com/jira/REST/cloud/#api/2 or similar - remember to prepend it with your jira URL

 

public static string RunQuery(string query, string argument = null, string data = null, string method = "GET")
{
try
{
m_BaseUrl = query;
HttpWebRequest newRequest = WebRequest.Create(m_BaseUrl) as HttpWebRequest;
newRequest.ContentType = "application/json";
newRequest.Method = method;

if (data != null)
{
using (StreamWriter writer = new StreamWriter(newRequest.GetRequestStream()))
{
writer.Write(data);
}
}

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)
{
//MessageBox.Show(@"There is a problem getting data from Jira :" + "\n\n" + query, "Jira Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}

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

1 vote
santhosh.gs February 8, 2021

We are unable to connect using create CreateRestClient and it says it deprecated for basic authentication with user id and password . Is there any code snippet to replace this for c# ?

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.
February 8, 2021

Hi @santhosh.gs 

You should be able to use the same code, just replace the user id with your email address that you log in to Jira with and replace the password with an API token created using the same email address. For further info on API tokens, see this doc 

Like Abdul Rehman Butt likes this
Stan St April 25, 2022

Did you succeed? I use Atlassian.SDK 8.6.0 library for .NET and get Unathorized 401 when passing username+token. It is fine when I pass username+password.

Like Abdul Rehman Butt likes this
0 votes
double0doug February 15, 2018

I'm working with this in VB and I don't know all the ins and out of C# accessibility modifiers.  I think what you want is Jira.CreateClient which has the signature we're looking for.  The documentation leaves a lot to be desired, so I just downloaded the source and worked it backwards using the unit tests.  

Since it's static I don't think you need to use the "new" keyword.

/// <summary>
/// Creates a JIRA rest client.
/// </summary>
/// <param name="url">Url to the JIRA server.</param>
/// <param name="username">Username used to authenticate.</param>
/// <param name="password">Password used to authenticate.</param>
/// <param name="settings">Settings to configure the rest client.</param>
/// <returns>Jira object configured to use REST API.</returns>
public static Jira CreateRestClient(string url, string username = null, string password = null, JiraRestClientSettings settings = null)
{
var services = new ServiceLocator();
settings = settings ?? new JiraRestClientSettings();
var jira = new Jira(services, new JiraCredentials(username, password), settings.Cache);
var restClient = new JiraRestClient(services, url, username, password, settings);

ConfigureDefaultServices(services, jira, restClient);

return jira;
}

 Versus what you're doing (and the documentation appears to be showing)

/// <summary>
/// Create a client that connects with a JIRA server with specified dependencies.
/// </summary>
public Jira(ServiceLocator services, JiraCredentials credentials = null, JiraCache cache = null)
{
_services = services;
_credentials = credentials;
_cache = cache ?? new JiraCache();

this.Debug = false;
}

 As an aside, the last time I worked with this library (several years ago)  your example would've worked.  I think the documentation (what tiny bit there is) is out of date.  

G Sri Hari April 4, 2018

I am using the same method which is CreateRestClient.

I am getting the results when using http jira url but it's not working when using https urls. 

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 14, 2017

This "jira" object you are creating here must come from a library or import somewhere.  You'll need to consult the docs for that to see what you need to feed into it.

sean o'brien July 14, 2017

There isn't any documents for this. 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 14, 2017

I'm a bit stuck then.  You need to know what to pass into this object and how to create those things, but if the library providing the object isn't documented, there's not a lot you can do.

Damian Grech August 21, 2017

I got the same issue, did you manage to find a fix for it?

Suggest an answer

Log in or Sign up to answer