Using personal access token to make REST APi call

Eunice October 6, 2021

Hi,

We use Jira on-prem and I am attempting to automate the export of a saved search query to csv. I have tried both Power Query in Excel and PowerShell. 

I was initially successful when using Power Query to retrieve the information, with my username/password specified as part of the process. However, this has since stopped working (400 error) since I tried to replace my password with the personal access token. Now it won't work even if I revert back to using my username and password, after clearing the local/global cache and resetting captcha. (Totally regret trying to change something that was working fine!!!)

This led me to try automating the process fully in PowerShell via Basic authentication with a base64 encoded username and password. When captcha is triggered I get a 403 error as expected, but a 401 error when captcha is reset. Script is as follows (note: i haven't tried piping the output into a CSV yet and may try a direct 'export to CSV' link in place of the current URL):

$user = 'username'
$token = 'token'

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

$encodedCred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($pair))

$basicAuth = "Basic $encodedCred"

$Headers = @{
Authorization = $basicAuth
}
Invoke-WebRequest -Uri 'https://<base_url>/rest/api/2/search?jql= <saved filter>`
-Headers $Headers

Interestingly when I check the usage of my token via the Jira UI, there is no record of it being used under the "last authenticated" column. Is there anything else that needs to be enabled for this to work?

0 answers

Suggest an answer

Log in or Sign up to answer