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.
Chris
SonarQube ID information
Server ID: 079D80A9-AXixKcPHMFvFFSaGUHiq
Version: 9.3.0.51899
# 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
$response