Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,846
Community Members
 
Community Events
168
Community Groups

Try to authenticate with JIRAPS or REST API

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

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

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>

Thanks, Jose. Worked for me :)

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)"

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.

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) )

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?

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

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

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.

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