gszalai
(Geza Szalai)
21 February 2020 16:36
1
Hi Team,
I’m tring to do a RestAPI POST to start a deployment but I’m getting 401 unauthorized error, the api key is for a user who is able to deploy on Octopus UI.
$apiKey=“API-” #octopus API key
$apiUrl=“http://server1/octopus/api ”
$headers = @{}
$headers.Add(“X-Octopus-ApiKey”,$apikey) #restapi header
$DeploymentBody = @{
ReleaseID = “Releases-7084”
EnvironmentID = “Environments-82”
FormValues = @{ # Here is where you declare all your Prompt variables.
“Variable1” = “SetVariable1”
“Variable2” = “SetVariable2”
ForcePackageDownload=“False”
ForcePackageRedeployment=“False”
UseGuidedFailure=“False” }
} #| ConvertTo-Json
Invoke-RestMethod -Uri “$apiUrl/deployments” -Method Post -Headers $Header -Body $DeploymentBody
Do you have any clue why I am getting the error?
Thanks!
Géza
Hi @gszalai ,
Thanks for getting in touch! This may be a Header Syntax issue rather than permissions related.
Try updating the header to the following format.
$Header = @{ “X-Octopus-ApiKey” = $apiKey }
Here’s a great example that should help you further,
##CONFIG##
$apiKey = "" #Octopus API Key
$OctopusURL = "" #Octopus URL
$ProjectName = "" #project name
$EnvironmentName = "" #environment name
$TenantID = "" #tenant ID, can be found in /api/tenants
$ReleaseVersion = "" #Version of the release you want to deploy. Put 'Latest' if you want to deploy the latest version without having to know its number.
##PROCESS##
$Header = @{ "X-Octopus-ApiKey" = $apiKey }
#Getting Project
Try{
$Project = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$ProjectName" -Headers $Header -ErrorAction Ignore| ConvertFrom-Json
}
Catch{
This file has been truncated. show original
Give that a try and let me know how it goes.
Tina