Newbie: .Net request to myself returning 401 error

Hi. New Here.

I am integrating Jira with a .Net Core MVC application (C#) with OAuth 2. I go through the redirect to atlassian.com to get permission from the user, and then exchanging the returned code for an access token. That all works fine.

When I attempt to make any request to the API with the just returned Access Token, I always get a 401 (Unauthorized) response.

I’m just running a POC at this time, so I chose to simply call “myself”. The scopes setup for my app are manage:jira-data-provider, read:jira-work, write:jira-work, read:jira-user, but I don’t think that matters at this point since this is an authentication issue, not a permissions issue (I think).

Here’s the code that makes the get request with the HttpClient object.

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Bearer”, jiraToken.access_token);
httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.Accept.ToString(), “application/json”);

var response = await httpClient.GetAsync(new Uri(“https://MyDomain.atlassian.net/rest/api/3/myself”));

By the way, I also tried “Basic” in the authentication header since I saw that in some articles, but seems to me that this is a Bearer token that I’m using.

Is there additional header information I need to pass? Anyone have some insight into how I can get past this early speed bump?

Thanks.

Welcome to the Atlassian Developer Community, @JeffCampbell!

With the assumption that the access token exchange is correct, the URI format you are using is incorrect for OAuth 2.0 apps. The format should be:

The URIs for OAuth 2.0 (3LO) app REST API calls have this structure:

https://api.atlassian.com/ex/jira/<cloudId>/rest/api/3/<resource-name>

For example, https://api.atlassian.com/ex/jira/35273b54-3f06-40d2-880f-dd28cf8daafa/rest/api/3/issue/DEMO-1

In your case, instead of https://MyDomain.atlassian.net/rest/api/3/myself, it should be https://api.atlassian.com/ex/jira/<yourCloudId>/rest/api/3/myself

For more information, kindly check this section.

Kindly try it out and let us know how it goes.

Ian

Yes. This is exactly what my problem was. Thanks for your help.

1 Like