Bitbucket Pipelines: JSM Alerts via PS Invoke-RestMethod

Suzanne August 15, 2024

We're trying to send an alert to JSM from Bitbucket Pipelines but receiving a "401 Unauthorized" error. Has anyone had any success authenticating from Bitbucket Pipelines to the JSM API using Powershell? Any ideas what I'm missing?

Pipeline code:

- $PAIR = [System.Text.Encoding]::UTF8.GetBytes("$env:API_USER:$env:API_TOKEN") 

- $HEADER = @{"Authorization" = "Basic " + [System.Convert]::ToBase64String($PAIR)}

- Invoke-RestMethod -Method Post -ContentType 'application/json' -Uri "https://api.atlassian.com/jsm/ops/api/xxxxxxxxxxxxxxxxxxxxxxxxxx/v1/alerts" -Headers $HEADER -Body '{ "message":"Deployment failed - Test", "responders":[ { "id":"xxxxxxxxxx", "type":"team" } ], "visibleTo":[ { "id":"xxxxxxxxxx", "type":"team" } ], "alias":"test3", "description":"Deployment test", "priority":"P1" }'

Triggers 401 Unauthorized:

Unauthorized.PNG

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 22, 2024

Hey @Suzanne ,

welcome to the Community!

The error message indicates an authentication issue, meaning the credentials are either invalid or not configured correctly in your script.

One difference I could spot is that in the first command you referenced the  variables (API_USER and API_TOKEN) with the $env: prefix which is required in Windows environment. However, in the second and third command you didn't provide the $env: prefix when referencing the variables PAIR and HEADER.

This may have caused the variable to not be used, and since those variables contains the authentication details, if they were not correctly referenced, that would explain the 401 authentication error.

With that in mind, I would recommend first trying to execute that script locally on your Windows machine to confirm if it's working properly and make the necessary adjustments. Once the script is working locally, you can update your Pipelines YAML file to match.

I hope that helps! Let us know in case you have any questions.

Thank you, @Suzanne !

Patrik S

Suzanne August 23, 2024

Hi Patrik, thank you for your suggestions. I was able to resolve my issue with Solved: Invoke-WebRequest API error 401 (Unauthorized) (atlassian.com)

Thank you,

Suzanne

 

Like Dave Rosenlund _Trundl_ likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events