Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 6-Nov-20 22:22pm

 
Share this answer
 
Hi Rick thanks for replying - I don't think it's security on Azure as I can get a token without any errors it's the call to the API that errors - I think the token is not arriving or something along those lines
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900