Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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?

4 answers

1 accepted

2 votes
Answer accepted
Warren Community Leader 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. 

0 votes

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 Community Leader Feb 08, 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 

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you