Connecting to JIRA via C# rest api

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?

3 answers

1 accepted

1 vote
Accepted answer
Warren Levy Community Champion Jul 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);
}

0 votes

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.

There isn't any documents for this. 

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.

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

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.  

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. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,817 views 18 22
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you