5

i´am trying to access a RESTAPI via Powershell. Within the login process an Authtoken is generated and is needed for any later command and has to be put into the header. So far nothing special.
Of course i want to put the generated Authtoken into a variable for easier handling. But i´am unable to do so ... Here is what i´am trying: Logging in and getting the Authtoken

$payload = @{"login"="username";"password"="password"}
$AuthToken = Invoke-RestMethod -Method Post -ContentType application/json -Body (ConvertTo-Json $payload) -Uri "https://path/to/api/login"

Because the API accepts the authoken only in a special form i have to edit it a little bit

$AuthToken = $AuthToken.Replace("auth_token=",'"auth_token"="')
$AuthToken = $AuthToken.Insert(73,‚"‘)

The Authtoken before

@{auth_token=rShln/Yc2cepDtzbNFntdZue:9c3ce025e5485b14090ca25500f15fa2}

and after my Treatment

@{"auth_token"="St6tecwEseAQegkfhACXUwaj:d7e3e2095ba31073e3fbc043c4563d28"}

If i manually insert the Authtoken into the Restmethod the call looks this:

Invoke-RestMethod -Method Get -ContentType application/json -Headers @{"auth_token"="JsRaTBRlElpq1jLLX5z3TXUy:91d0e1eee1943f6cd6dbaa1d0b9ba9d0"} -Uri "https://path/to/api/something"

As you may gues this works pretty well! If i now try use the Authtoken from my variable my Rest Call looks like this:

Invoke-RestMethod -Method Get -ContentType application/json -Headers $Authtoken -Uri "https://path/to/api/something"

Powershell gives me the following error

Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "@{"auth_token"="St6tecwEseAQegkfhACXUwaj:d7e3e2095ba31073e3fbc043c4563d28"}" value of type "System.String" to type 
"System.Collections.IDictionary".
At C:\Users\User\Desktop\xxx.ps1:6 char:70
+ ... -Method Get -ContentType application/json -Headers $AuthToken -Uri "h ...
+                                                        ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I have no clue why i´am getting this error and would be so thankfull i someone could help me out on this!

1
  • Use it like this: $Headers = @{"auth_token" = $Authtoken} Commented Jul 1, 2021 at 7:05

5 Answers 5

1

I had this issue when using airtable api. what i did was just curl "https://api.airtable.com/v0/app----/Post?maxRecords=3&view=Grid%20view" -H @{"Authorization" = "Bearer Your-Api-here"}

And it worked

Iam a newbie here, i just post this here thinking it might help someone, it took me hours to discover this.

Sign up to request clarification or add additional context in comments.

Comments

1

Looks like this is an insane PowerShell bug. If you have a tab after -Header $Headers, the casting to Collections.IDictionary fails because the tab is removed somewhere inside. I'll raise an issue on Github. Solution is to make sure that there are no tabs between your parameters. They may look the same

Issue raised in PWSH github: https://github.com/PowerShell/PowerShell/issues/15943

Comments

0

So right now it looks like $AuthToken is a string. The string is formatted like you would expect a Hashtable to be, but I don't think it's actually a hashtable. What you can do to fix this is to use Invoke-Expression on the string, and that will convert it to an actual hashtable for you. Something like:

$AuthToken = Invoke-Expression $AuthToken
Invoke-RestMethod -Method Get -ContentType application/json -Headers $Authtoken -Uri "https://path/to/api/something"

4 Comments

Thanks for your help but this did not solved the problem :( The error looks now a bit different but it´s kinda still the same: Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "System.Collections.Hashtable" value of type "System.String" to type "System.Collections.IDictionary". At line:1 char:70 + ... -Method Get -ContentType application/json -Headers $AuthToken -Uri "h ...
You where kinda right. It needs to be a hashtable to use it as a Header. But i handled this in another way since Invoke-Expression has not worked
To bad you didn't post your "another way" to solve this. I have the exact same problem.
I have the same problem as well. How do I do it?
0

The Header should be in System.Collections.IDictionary method (check : https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.system-collections-idictionary-add?view=netframework-4.8)

Example: Am doing it on a centreon api
The header Variable should be: $headers = @{"centreon-auth-token" = "$Key"}
The key contain only the token, example: NWU5ZDY5ODFiNWI2YTYuMzAzNjM3NDI=

Comments

0

Had a similar issue while trying to run through a tutorial for building APIs with Python (Auth0).

Was able to create a work around by putting the commands into a .sh file, and ran that. Worked running the file.

@James Lear appears the bug is still there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.