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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Invoke-WebRequest API error 401 (Unauthorized) Edited

Dear community, 

I'm having trouble connecting to my companies Jira server using a powershell script and the Invoke-WebRequest commandlet.

I'm using the following script. Note that I'm getting the same error with any valid URI to this Jira server. While the same user can access all these URI's through a webbrowser just fine.

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

$baseUrl = "https://MyDomain.atlassian.net"
$jql = "%22Epic Link%22 = SD-2"

$user = 'USER@MyDomain.com'
$pass = 'TheCorrectPassword'

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

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

$basicAuthValue = "Basic $encodedCreds"

$authHeader = @{
Authorization = $basicAuthValue
}

Invoke-WebRequest -uri $($baseUrl + "/rest/api/latest/search?jql=$jql") -Headers $authHeader -UseBasicParsing -ErrorAction Stop -Verbose

Output from powershell:

Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At D:\Work\JiraAccess2.ps1:22 char:1
+ Invoke-WebRequest -uri $($baseUrl + "/rest/api/latest/search?jql=$jql ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Could there be a setting on the Jira Server that prevents this user from connecting via "Invoke-WebRequest"? Or is something wrong with my script?

Edit: I'm using the cloud version.

1 answer

1 accepted

0 votes
Answer accepted
Andy Heinzer Atlassian Team Sep 28, 2020

Hi Christian,

I see that you're using powershell in order to try to connect to the Jira Cloud REST API, but that you're getting a 401 authorization error.  Thanks for posting your code here, I think I see the problem.

The way you're converting the password appears to be calling the ASCII encoding of the text.  However we're actually expecting UTF-8 to be used here when converting.  We have a sample of this for powershell over in Basic auth for REST APIs.

$Text = ‘user@example.com:api_token_string’
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
$EncodedText = [Convert]::ToBase64String($Bytes)
$EncodedText

I'm betting that using the ASCII encoding here is actually generating a different character string when encoded into base64.  In turn your encoded credentials are incorrect here and Jira is throwing a 401 error.  Try using this example as a base and see if you have any better success here. 

Let me know the results.

Andy

Thank you very much Andy. Using the api token instead of the password and using UTF-8 fixes my problem.

In case it helps someone else in the future, this is the script that works for me now:


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

$baseUrl = "https://MYDOMAIN.atlassian.net"
$jql = "%22Epic Link%22 = SD-2"

$user = 'USER@MYDOMAIN.com'
$token = 'myToken'

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

$encodedCreds = [System.Text.Encoding]::UTF8.GetBytes($pair)

$encodedBase64 = [System.Convert]::ToBase64String($encodedCreds)

$basicAuthValue = "Basic $encodedBase64"

$authHeader = @{
Authorization = $basicAuthValue
}

Invoke-WebRequest -uri $($baseUrl + "/rest/api/latest/search?jql=$jql") -Headers $authHeader -ErrorAction Stop -Verbose

 

Like Andy Heinzer likes this
Andy Heinzer Atlassian Team Sep 29, 2020

Thanks for posting back with your code.  Feel free to accept my answer if it solves your problem here.  This can help others to easily see the solution that might have the same problem in the future.

You're also welcome to post your solution as an Answer to this question, instead of a reply.  That way you can also accept your own answer too.

Cheers,

Andy

Like Christian Thurow likes this

Hi All,

I have a similar situation but I'm trying to use the ConfluencePS PowerShell module. I tried your example above using the REST API and I was successful in logging in and executing a query.

I'm unsure how to implement this method using the Set-ConfluenceInfo command as shown here: Set-Info - Docs | AtlassianPS.

I have tried to use a similar method as detailed here to create a credential object but I'm still unable to run Get-ConfluenceSpace without the error Christian mentioned in the original post.

Any help would be greatly appreciated.

 

I should also mention that I was successful in connecting using my generated token and I would like to use my username/password with the PowerShell module.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

4,571 views 12 8
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you