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:
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.