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

Powershell Invoke-RestMethod returns 401

Rune Rasmussen January 20, 2021

For starters
I'm very new to the world of web requests, REST, and APIs, and only have limited experience with Powershell, so it may be that I'm missing something very obvious to the trained eye.

Situation
I can make a REST call in Postman, to our Jira Cloud instance, and the the results of a query to our knowledgebase.

Basically this URL:
https://MYSITE.atlassian.net/rest/servicedeskapi/servicedesk/1/knowledgebase/article?query=Skype

When trying to do the samething with Invoke-RestMethod in Powershell I keep getting a 401 Unauthorized response.

The things I've tried
Read through every hit on the first couple of pages on Google and Bing (because you never know), but haven't found anything that made it clear to me.

My code
Gracefully stolen from someone with a similar issue, who got it to work like this.

$user = [System.Text.Encoding]::UTF8.GetBytes("my.email@company.dk:MyApiToken")
$headers = @{Authorization = "basic" + [System.Convert]::ToBase64String($user)}

Invoke-RestMethod -Uri "https://MYSITE.atlassian.net/rest/servicedeskapi/servicedesk/1/knowledgebase/article?query=Skype" -Method GET -Headers $headers -ContentType "application/json"

 

Why Powershell?
I want to create a one-stop-script for creating new projects, and access to our instance is managed by groups in Azure AD. We already have a script for creating those groups, and this would be a continuation of that script.

NOTE:
This is just me learning. The GET call is just to help me get a better understanding of working with REST calls in Powershell.

1 answer

1 accepted

1 vote
Answer accepted
Rune Rasmussen January 21, 2021

Update:

I woke my laptop up from sleep mode, noticed a capital B in "Basic", changed it to lower case, looked at headers in Postman, noticed that the Authorization header had "Basic" with a capital B in the value, changed it back to capital B in my script.
Then I ran the script to remind myself what error message I was getting, and now it works.

I don't know why, but for some reason it works.

Earl McCutcheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 21, 2021

Hey @Rune Rasmussen ,

Glad you figured this one out and a big thank you for sharing the solution you found.

And I too am not 100% sure on what is happening here but the third party applications are doing some case conversion in the background causing a conflict for the auth header and its a mind your p's and q' moment, but I had helped on a similar case with postman with a similar issue on case sensitivity in "basic" depending on whether you're currently logged in or not, and I wanted to link it here as a reference point in case anyone else is hitting the same issue:

Regards,
Earl

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events