API calls with powershell not working (401 unauthorized)

Hi, newcomer to SonarQube and the community here, and i think this is probably a very basic question - although i’ve been unable to find the solution in the documentation or the forum posts.

I’m trying to connect to the SonarQube API of the on-prem instance of my organisation using the powershell script below (having substituted the variables for my own instance etc). I keep getting 401 unauthorised when running the script, but when I paste the generated URL into a browser (assuming I’m logged in to the site) I can get a successful response from the sonarqube server.

How do I get this to work with powershell?
I’ve also tried adding the cert thumbprint into the API call, and also some attempts at Base64 encoding the token but no luck there either.

Any help appreciated.

SonarQube ID information
Server ID: 079D80A9-AXixKcPHMFvFFSaGUHiq

# SonarQube API Endpoint
$sonarQubeUrl = "https://my-sq-instance.net"
$apiEndpoint = "$sonarQubeUrl/api"
# SonarQube Project Key
$projectKey = "MySonarQubeProject"
# Authentication Token
$token = "my-authentication-token"
# API Request Headers
$headers = @{
    'Authorization' = "Bearer $token"
# Example: Get Project Information
$projectInfoUrl = "$apiEndpoint/project_analyses/search?project=$projectKey&ps=1"
$response = Invoke-RestMethod -Uri $projectInfoUrl -Headers $headers -Method Get -Verbose
# Display the response

Hey there.

You can see an example of basic authentication using Powershell here:

And, your version is past EOL. You should upgrade to either the latest version or the current LTS at your earliest convenience. Your upgrade path is:

9.3 → 9.9.3 → 10.3 (last step optional)

You may find these resources helpful:

If you have questions about upgrading, feel free to open a new thread for that here.

Hi, thanks for the input.
The key difference in the example you linked to seemed to be incorporating the (empty) password.
And maybe also the use of ‘basic’ authentication rather than ‘bearer’, although i may experiment with these now i have it working.
Anyways, I’m now getting a response, so thanks for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.