I am writing a powershell script to request a access token. When I run it I am getting the error below for Invalid Grant type. I am not sure why (I am fairly new to powershell programming).
Error:
Invoke-WebRequest : Invalid grant type
At C:\pwscripts\testps.ps1:34 char:1
+ Invoke-WebRequest -uri 'https://urlsite' -M ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The code I am using:
$BodyArray = @(
@{
grant_type= 'refresh_token'
refreshToken= 'refres token here'
}
)
$BodyArray_Json = ConvertTo-Json -InputObject $BodyArray
(New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
Invoke-WebRequest -uri 'https://urlsite' -Method POST -body $BodyArray_Json -Headers @{'Accept' = 'application/json';'Content-Type' = 'application/json';}