Invoke-RestMethod authorization problem

Product: PowerShell Universal
Version: 4.0.2

Hi,

I can run this script from a powershell window with no problem:

Invoke-RestMethod https://nsd.us002-rapididentity.com/api/rest/admin/connect/exportProject -Authentication “Basic” -Credential $cred -OutFile “main.zip”

but when I add it to a PSU script like this:

$response = Invoke-RestMethod https://nsd.us002-rapididentity.com/api/rest/admin/connect/exportProject -Authentication “Basic” -Credential $secret:ricloud -OutFile “main.zip”

I get this error:

[error] {“httpStatusCode”:401,“message”:“Insufficient Authorization”,“details”:“The Authorization HTTP Header is missing or malformed”}

I know $secret:ricloud is correct, because I use it in several endpoint scripts. Any idea why I am getting this error?

cheers,
ski

Ok, this is strange. I can get it to work with a Post method. The file is downloaded ok, but I do get a 405 error as the endpoint I am using doesn’t like Post methods. For now I can do this, but I would like to know why the Get method does not work

You could try to use cookie auth directly. I’m not sure why what you are trying isn’t working.

        Invoke-RestMethod  https://nsd.us002-rapididentity.com/api/v1/signin -Method Post -Body (@{
                Username = $secret:ricloud.UserName
                Password = $secret:ricloud.GetNetworkCredential().Password
            } | ConvertTo-Json) -SessionVariable Session -ContentType "application/json"

$response = Invoke-RestMethod https://nsd.us002-rapididentity.com/api/rest/admin/connect/exportProject -WebSession $Session -OutFile “main.zip”

Adam,

Figured this out. The powershell window was caching a different set of credentials. My bad.

cheers,

ski

1 Like