Hi all, I am trying to make a call to an API protected with a Azure AD token from Powershell
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Token = Get-AzCachedAccessToken
$headers.Add("Authorization",$Token)
$response = Invoke-RestMethod "http://mysite.com:9666/getvalves" -Headers $headers
$response | ConvertTo-Json
The code in Get-AzCachedAccessToken was something I downloaded and I believe it works
I receive this error when running the above
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At C:\Users\pete\OneDrive\Documents\WindowsPowerShell\Scripts\MyScripts\CallAlvalvesAPI.ps1:7 char:13
+ $response = Invoke-RestMethod "http://mysite.com:9666/getva ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
So it is obviously not authorizing successfuly
I can access the API from several C# apps without a problem - it just would be handy to be able to test new stuff from Powershell
What I have tried:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Token = Get-AzCachedAccessToken
$headers.Add("Authorization",$Token)
$response = Invoke-RestMethod "http://mysite.com:9666/getvalves" -Headers $headers
$response | ConvertTo-Json