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

Bamboo Rest API always returns 401 from Powershell

Ma Ki
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!
May 15, 2024

Hello,

i´m trying to access the Bamboo REST API from Powershell. I want to automate some configurations. When i send the Request with a tool like Bruno or Postman i will receive an correct answer. When using the exact same Request from Powershell (From within VSCode or native PowerShell cmd window) i always get an error "System.Net.WebException: Der Remoteserver hat einen Fehler zurückgegeben: (401) Nicht autorisiert."

I tried to use Invoke-WebRequest and/or Invoke-RestMethod. I encoded the Token in UTF-8, used EscapeDataString, [Net.SecurityProtocolType]::Tls12, Token in Single-Quote or Double-Quote and so on. I´m not able to get this working from Powershell. Is there any advice where to search for any clue?

Here is my Powershell Code:

$token = "<MyToken>" # Access Token from Bamboo Profile
$bambooheaders = @{
  Authorization = "Bearer $token"
  "Content-Type"="application/json"
}

$url = 'http://<MyURL>/rest/api/latest/deploy/project/forPlan?planKey=PDCS-ZWZ'

try {
  $Response = Invoke-WebRequest -Uri $url -Headers $bambooheaders -Method Get
  Write-Host $Response
} catch {
  Write-Host $_.Exception  -ForegroundColor Red;
}

Any help is much appreciated, since this is very frustrating.

1 answer

0 votes
Shashank Kumar
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 15, 2024

Hello Ma Ki,

Welcome to Atlassian community

Can you use the above REST API as Curl in your powershell script task and let me know if you still see 401 error?

curl -H "Authorization: Bearer token_value" \
-H 'Content-type: application/json' \
-H 'Accept: application/json' \
-X GET http://MYURL/rest/api/latest/deploy/project/forPlan?planKey=PDCS-ZWZ

Replace token-value and MYURL with correct values.

Regards,

shashank kumar

**please don't forget to Accept the answer if your query was answered**

 

Ma Ki
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!
May 27, 2024

Hello.

Sorry for the late Response. The curl is the same as the Invoke-WebRequest. And even when trying i still get the error.

BUT, i have solved it by coincidence. I tried some testings with Bruno and Postman in order to get multiple requests been sent out with different values. Within Postman i discovered that there is always an additional request, which said that the request has been forwarded. Delving into the Console Logs i found the issue. The URL has to be slightly altered in two ways:

- It has to be https instead of http
AND
- you have to set the port (although it is default) to :443

-> https:\\<url>:443\...

After that the request worked fine.

In the end a very simple solution, but finding the real error was a bummer.

Like Shashank Kumar likes this
Shashank Kumar
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 27, 2024

Thanks for letting us know the solution. Glad to know the issue is now solved! 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events