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!
$Headers = @{"auth_token" = $Authtoken}