Jira REST API returning 401 Unauthorized

Austin Luu July 5, 2019

Hello,

I can't seem to make any API calls to Jira using PowerShell, always getting 401 status code returned. Can anyone spot what I'm doing wrong?

$user = 'austin.luu91@gmail.com'
$pass = #SomePassword

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri 'https://15sof1.atlassian.net/rest/api/2/search?jql=project = CIT AND issuetype = "JIRA Request" AND (created >= startOfDay(-30d) OR updated >= startOfDay(-30d))' `
-Headers $Headers
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At line:1 char:1
+ Invoke-WebRequest -Uri 'https://15sof1.atlassian.net/rest/api/2/searc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Thanks! 

2 answers

1 accepted

0 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 26, 2019

Hi all,

I was working with Austin on a related post and I think we found the solution.

Atlassian Cloud recently deprecated support for TLS 1.0 and 1.1 protocols.  This will force all clients to support TLS 1.2 in order to connect. Deprecating TLSv1 and TLSv1.1 for Atlassian Cloud Products has more details.

While the planned effective date in that page was December 1, 2018, you can see in the recent Jira Cloud blog updates in https://confluence.atlassian.com/cloud/blog/2019/07/atlassian-cloud-changes-jul-1-to-jul-8-2019

... after consultation internally and with customers, we have allowed a silent/soft grace period of 6 months to allow customers additional time to update the necessary tools and systems. This grace period has now expired. 

In my search I came across this post:

https://www.codyhosterman.com/2016/06/force-the-invoke-restmethod-powershell-cmdlet-to-use-tls-1-2/

The issue, as I understand it, is that PowerShell by default uses TLS 1.0 for web requests, which will not work in our case. So this needs to be changed. Thankfully, this is an easy change. Just add the following line to your scripts:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Which means that we would need to make sure powershell is using the TLS v1.2 protocol when trying to connect to an Atlassian Cloud address, or else it won't be able to connect.

Cheers,

Andy

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 5, 2019

Hello,

Make sure that instead of your real password, you send your API token.

https://confluence.atlassian.com/cloud/api-tokens-938839638.html

Austin Luu July 10, 2019

Yes, it doesn't seem to work even with my API token. Though it seems like I'm getting a different error than before..

$user = 'austin.luu91@gmail.com'
$pass = 'apiToken'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri 'https://15sof1.atlassian.net/rest/api/2/search?jql=project = CIT AND issuetype = "JIRA Request" AND (created >= startOfDay(-30d) OR updated >= startOfDay(-30d))' `
-Headers $Headers\
Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
At line:1 char:1
+ Invoke-WebRequest -Uri 'https://15sof1.atlassian.net/rest/api/2/searc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand 
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2019

Cloud you change your URL to this one

https://15sof1.atlassian.net/rest/api/3/....... (etc)
Austin Luu July 11, 2019

Getting same SSL/TLS error with that url as well.. :/

Suggest an answer

Log in or Sign up to answer