Try to authenticate with JIRAPS or REST API

Titan Pro IT October 18, 2019

PS C:\WINDOWS\system32> New-JiraSession -Credential $cred
WARNING: JIRA returned HTTP error 401 - Unauthorized

 

I have tried using username, email account, Atlassian password and API token.  All fail.

I was getting:

ARNING: JIRA returned HTTP error -
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\PSJira\1.2.5.251\Public\New-JiraSession.ps1:92 char:13
+ $readStream = New-Object -TypeName System.IO.StreamReader ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

 

And then I saw someone got further by using:

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

 

And now I only get:

WARNING: JIRA returned HTTP error 401 - Unauthorized

 

I see may articles about deprecating basic authentication.  But using the token for the password is not helping.

 

 

5 answers

1 vote
Matt Kelley February 24, 2022

The issue is the way the header is being formed in the module. If you change the Authentication piece of the header in the Invoke-WebRequest function of the JiraPS.psm1 module file you can get it to work with a token. I would suggest a modification request be put in to update the JiraPS module to allow another parameter or a flag to permit the call to choose between bearer and basic authentication header. Here is the change I made to the module in order to get it to work with a PAT. Comment out the existing and paste in the second line shown below.JiraPS.JPG

(line 7388 in the Invoke-WebRequest function)
#$Headers["Authorization"] = "Basic $($SecureCreds)"
$Headers["Authorization"] = "Bearer $($Credential.GetNetworkCredential().Password)"

Matt Kelley February 24, 2022

Just a note: if you change this in your psm1 file, it will most likely break basic HTTP auth with userid/pwd. You may just want to include a local version of this function at the top of your powershell script that uses a PAT, and leave the base psm1 file alone. That way you just essentially override the function in the module with your local version. Just make sure if you script calls import-module JiraPS you do that BEFORE you put a local version of the invoke-webrequest function or you will still be using the module version.

Like Will C likes this
Lucien Janssens July 6, 2022

I keep getting an error:

Invoke-JiraMethod : Basic authentication with passwords is deprecated. For more information, see:
https://developer.atlassian.com/cloud/confluence/deprecation-notice-basic-auth/

Does anyone know how to authenticate with an API key?  

I also tried 

Invoke-RestMethod -uri '<cloudname>.atlassian.net/rest/auth/1/session' -Credentials $cred  
(where $cred is user name , password = API key) 
Throws an error 401 (unauthorised) 

(see also JIRA 7.0.0-SNAPSHOT (atlassian.com) )

Matt Kelley July 6, 2022

If you are certain you are making the API call correctly (the basic auth error you are getting from Invoke-JiraMethod implies you are not using bearer auth) and getting a 401, I would check the API key to make sure it is not expired or revoked. Maybe generate a new token and retry?

Lucien Janssens July 6, 2022

Hi Matt,

Thanks for your reply. 
With some trial and error, I managed to get it to work with the below powershell code.
(No changes in the JiraPS module)
I hope that others can benefit.

Import-Module JiraPS  # (installed with Install-Module -Name JiraPS )

$credential = Get-Credential -UserName 'JiraTicketCreator@contoso.com' -Message "Use account's API TOKEN as a password!"

Set-JiraConfigServer 'https://contoso.atlassian.net'  # required since version 2.10

New-JiraSession -Credential $credential

$params=@{

Project="TST"

IssueType="Support"

Summary = "Powershell created support request summary"

Description = "Some description"

 }

 
New-JiraIssue @params

 

Key              Summary                                                  Status                   Created

---              -------                                                  ------                   -------

TST-2            Powershell created support request summary               Waiting for support      7/6/2022 1:51:13 PM

 

Like Matt Kelley likes this
Will C
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.
November 11, 2022

@Matt Kelley Thanks for tip with ammending the jiraps module, saved me so much time trying to use a PAT :)

1 vote
Jose Elizondo October 23, 2019

After reading many blogs and pages I end up with this and works.

# Use https
Set-JiraConfigServer 'https://youcompany.atlassian.net/'

# For credentials use email address and API Token
$cred = Get-Credential -Message 'Atlassian Credentials' -UserName 'your.email@yourcompany.com' #For password use the API Token

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
New-JiraSession -Credential $cred

Randal Schmidt November 12, 2019

I tried it and it still does not work:

 

PS C:\WINDOWS\system32> # For credentials use email address and API Token
$cred = Get-Credential -Message 'Atlassian Credentials' -UserName 'myemail@mycompany.com' #For password use the API Token

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
New-JiraSession -Credential $cred
DEBUG: [Get-JiraConfigServer] ParameterSetName: __AllParameterSets
DEBUG: [Get-JiraConfigServer] PSBoundParameters:
Key Value
--- -----
ErrorAction Stop

 

DEBUG: [New-JiraSession] ParameterSetName: __AllParameterSets
DEBUG: [New-JiraSession] PSBoundParameters:
Key Value
--- -----
Credential System.Management.Automation.PSCredential

 

DEBUG: [Invoke-JiraMethod] ParameterSetName: __AllParameterSets
DEBUG: [Invoke-JiraMethod] PSBoundParameters:
Key Value
--- -----
Credential System.Management.Automation.PSCredential
Method Get
Headers {}
URI https://mycompany.atlassian.net/rest/api/2/mypermissions
StoreSession True

 

Resolve-DefaultParameterValue : Cannot bind argument to parameter 'Reference' because it is null.
At C:\Program Files\WindowsPowerShell\Modules\JiraPS\2.12.4\JiraPS.psm1:3215 char:78
+ ... ultParameterValue -Reference $global:PSDefaultParameterValues -Comman ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Resolve-DefaultParameterValue], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Resolve-DefaultParameterValue

Cannot index into a null array.
At C:\Program Files\WindowsPowerShell\Modules\JiraPS\2.12.4\JiraPS.psm1:3222 char:9
+ $_headers = Join-Hashtable -Hashtable $script:DefaultHeaders, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\JiraPS\2.12.4\JiraPS.psm1:3266 char:13
+ if ($_headers.ContainsKey("Content-Type")) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

DEBUG: [Test-ServerResponse] Investigating $InputObject.Headers['X-Seraph-LoginReason']
DEBUG: [New-JiraSession] Adding session result to existing module PrivateData

PS C:\WINDOWS\system32>

Andy Myatt March 8, 2021

Thanks, Jose. Worked for me :)

0 votes
Matt Kelley February 24, 2022

duplicate

0 votes
Randal Schmidt November 27, 2019

The upgrade to the latest version fixed it.  Thank you.

Randal Schmidt February 3, 2020

This is now doing the same thing.  It seems I can log in once with JiraPS and not again for several hours.  I cannot find a way to disconnect whatever is locking me up with Jira.  I have tried it on two computers with the same results.

0 votes
Titan Pro IT October 22, 2019

I was able to get a REST call using Invoke-WebRequest with my email and API token to connect with a return code of 200.  But putting the same in with:

 

New-JiraSession -Credential $Global:cred

 

returns:

WARNING: JIRA returned HTTP error 401 - Unauthorized

 

So is the API call under the hood buggy for JIRAPS?

Suggest an answer

Log in or Sign up to answer