Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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 # people like 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
Published in Jira Work Management

Jira Work Management FAQ

Welcome to the Jira Work Management FAQ We're glad you're here. If you haven't already, watch our video to get a one-minute overview of Jira Work Management. Check out some basic technical and logi...

7,301 views 50 25
Read article

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