Hi, I've been using the code that @Warren provided (which has been really helpful so far!) but I'm a bit stuck and I was hoping that someone can help and show me where I'm going wrong!!
My full code is below. Few things I've had to add/alter.
I replaced GET with PUT in my request:
string resultsReturned = RunQuery("rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=5&startAt=0", "", "", "PUT");
Because when I leave GET in place I get this error:
{"Cannot send a content-body with this verb-type."}
I also had to put this in as I couldn't get a tls/ssl connection without it:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
However I'm still not returning any data. I'm very stuck! Can someone help me?
Full code that I have:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JIRA_service_desk
{
public partial class WebForm9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string resultsReturned = RunQuery("rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=5&startAt=0", "", "", "PUT");
resultsTest.InnerHtml = resultsReturned;
}
public static string RunQuery(string query, string argument = null, string data = null, string method = "GET")
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string m_BaseUrl = "https://myurl.ac.uk/";
try
{
m_BaseUrl = 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 ex)
{
Console.WriteLine(ex);
throw;
}
}
private static string GetEncodedCredentials()
{
string m_Username = "hollie@email.co.uk";
string m_Password = "";
string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
}
}
Hi @HH
You haven't actually said what your issue is at the moment, but I'm guessing that you're receiving a "cannot be authorized" error. If so, you will need to look at this because you can't use password anymore, it has to be an API token.
If you're already using the token and are having some other issue, tell us what it is and we can try to get you further along the API road. Either a screenshot or copy of the message / what is happening or not happening
Also, I don't think that PUT is correct, it should be GET for getting details - PUT or POST are for adding details to a new or existing ticket
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Warrenthanks for the reply!
My problem is that I'm not returning anything at all! I get an empty string :(
If I go to that URL directly I definitely get results!
I'm using a token and my email address to connect - I found that bit of advise on another post that I think you did :) However on this url:
https://id.atlassian.com/manage/api-tokens?_ga=2.72468315.930218930.1561976285-428222862.1507889501
It says it's never been accessed - so I'm guessing my authentication isn't correct!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @HH
To rule out issues with the code, you could try using Postman (either the web version or download it) so that you're just checking the authentication aspect. I'll try a bit later to take your code and see if it works for me, just need to finish something else first. Let me know if you get anywhere before I get to it.
Two other things I've noticed about your original post, you're using server whereas I'm on cloud, but that shouldn't make much difference. Also, your "namespace" has service desk in the wording - are you doing this for service desk? Because again I don't use it, I'm on Jira Software and I don't know much about the API for SD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Warren I'll give postman a go - thanks!
Yes I am using Server.
We are using JIRA service desk and JIRA software. I'm attempting to connect to both without any luck! :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @HH
I used your code and got exactly the same error messages as you did, but I now have it working.
Change the PUT back to GET and replace the empty strings with null, so it looks like
string resultsReturned = RunQuery("rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=5&startAt=0", null, null, "GET");
Using this on a Jira Software project works for me, so hopefully for you as well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Warren I used Postman and that has really helped! It's showing that my authentication is failing. I'm going to try and tackle why that is today and hopefully get it sorted! Then I'll change the PUT back to GET and have a go with those changes as well.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Warren ok so not sure why the authentication is failing!
For the username are you using an email address and for the password a token that you've generated here:
https://id.atlassian.com/manage/api-tokens
I've made sure the account is an administrators account - but I haven't done anything to it from within the application.
Do you know if I'm missing a step? I don't seem to be accessing the token!! :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Warren - I think the authentication difference is between Server and Cloud. Found a great article explaining that creating tokens for a server instance is different:
https://community.atlassian.com/t5/Jira-questions/How-to-authenticate-to-Jira-REST-API/qaq-p/814987
Thanks for all your help! :) Once I can sort authentication I think your code examples will be really useful!
:)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @HH
Yes, the username must be the Jira e-mail that the API token was generated with. Hopefully you get it sorted soon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.