How to get userid by email using API?

Christoph Roth February 1, 2018

Hello,

I am developing an Application that creates Issues in Jira, and I need to get the username of the current user by email. 

I already found this. When I open this query in my browser, for example:

https://jira.mycompany.de/rest/api/2/user/search?username=email@mycompany.de

this works fine, but only while I am logged in in Jira. 

However, 

when I try to use this in my code, it returns no result.

var url = "https://jira.mycompany.de/rest/api/2/user/search?username=" + email;
var result = new WebClient().DownloadString(url);

I know that this resource cant be accessed anonymously, as stated in the documentation.

How can I do this programmatically? 

 

2 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
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 1, 2018

You would need to pass the Authorizaiton header to your request. You can read more here:

https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/

Go to the Supplying basic auth headers part.

Christoph Roth February 1, 2018

Could you show me an example on how to use this in C#?

Alexey Matveev
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 1, 2018

Sorry, I am a Java developer :)

Christoph Roth February 1, 2018

Nevermind, I was able to figure it out myself :)

 Here is what I did, in case anyone might need it in the Future. 

 var url = "https://jira.mycompany.de/rest/api/2/user/search?username=" + email;

string header = "Username:Password";

string encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(header));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", "Basic " + encoded);
request.PreAuthenticate = true;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
var result = reader.ReadToEnd();
}

 

Thank you for your assistance :)

Like # people like this
vishesh_kumar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 13, 2021

Finally working. Thanks

0 votes
wgakuru April 17, 2023

This solution no longer works: 

{"errorMessages":["The query parameter 'username' is not supported in GDPR strict mode."],"errors":{}}
Like # people like this

Suggest an answer

Log in or Sign up to answer