I am trying to list policy incidents with api, but I am getting below error. Could you please let me know what is wrong in the below code?
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
At line:7 char:1
+ [system.Net.WebResponse] $httpResponse = $httpRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Below is the code
----------------------------------
$oauthRefreshToken = "XXXXXXXXX"
$oauthUrl = "https://us-3.rightscale.com/api/acct/106079/oauth2"
$postString = "grant_type=refresh_token;refresh_token=$oauthRefreshToken"
$postBytes = [System.Text.Encoding]::UTF8.GetBytes($postString)
$httpRequest = [System.Net.WebRequest]::Create($oauthUrl)
$httpRequest.Method = "POST"
$httpRequest.headers.Add("X_API_VERSION", "1.0")
$httpRequest.ContentLength = $postbytes.Length
$requestStream = $httpRequest.GetRequestStream()
$requestStream.Write($postBytes, 0, $postBytes.length)
[System.Net.WebResponse] $httpResponse = $httpRequest.GetResponse()
$responseStream = $httpResponse.GetResponseStream()
[System.IO.StreamReader] $streamReader = New-Object System.IO.Streamreader -ArgumentList $responseStream
$Access_Token = $streamReader.ReadToEnd()
write-host $Access_Token.access_token
$httpRequest = [System.Net.WebRequest]::Create("https://governance-3.rightscale.com/api/governance/projects/106079/incidents")
$httpRequest.Method = "GET"
$httpRequest.Headers.Add("X_API_VERSION","1.0")
$Context = "bearer $accessToken"
$Context = $Context.Trim()
$httpRequest.Headers.Add("Authorization", $Context)
[system.Net.WebResponse] $httpResponse = $httpRequest.GetResponse()
$responseStream = $httpResponse.GetResponseStream()
[System.IO.StreamReader] $streamReader = New-Object System.IO.Streamreader-ArgumentList $responseStream
$Incidents = $streamReader.ReadToEnd()
Thanks
Suman
‎Feb 07, 2020 02:27 AM
Hi Suman
Here's an example on how to gather the incidents data from Governance.
$accountId = "<YOUR ACCOUNT ID>"
$refreshToken = "<YOUR REFRESH TOKEN>"
$cmEndpoint = "us-3.rightscale.com"
$oauthResult = Invoke-RestMethod -UseBasicParsing `
-Uri "https://$cmEndpoint/api/oauth2" `
-Method Post `
-Headers @{
"X_API_VERSION"="1.5";
"X-ACCOUNT" = $accountId} `
-ContentType "application/json" `
-Body $(@{
"grant_type"="refresh_token";
"refresh_token"=$refreshToken
} | ConvertTo-Json)
$accessToken = $oauthResult.access_token
$shard = $cmEndpoint.Split('.').Split('-')[1]
$govEndpoint = "governance-$shard.rightscale.com"
$incidents = Invoke-RestMethod -UseBasicParsing `
-Uri "https://$govEndpoint/api/governance/projects/$accountId/incidents" `
-Method Get `
-Headers @{
"API-VERSION"="1.0";
"Authorization"="Bearer $accessToken"
} `
-ContentType "application/json"
Let me know if it helps.
‎Feb 07, 2020 01:13 PM