Reputation: 11
I am attempting to run a cURL command in PowerShell using the Invoke-RestMethod cmdlet but it will not work properly.
It connects to the server and the API key is accepted. However, the credentials are not being passed correctly and I am receiving a response of
{"error":"Invalid user . Can't find credentials."}
When I run the following cURL command in cmd:
curl -X POST "https://valid_URI" -H "Content-Type: application/json" -H "x-api-key:KEY" -d "{"client_id":"VALID_ID","client_secret":"SECRET"}"
I get the expected response:
{"auth_token": "TOKEN"}
Great, it works!
From here I wanted to automate the response because a token is only good for 24 hours so I figured PowerShell would be a good route using the Invoke-RestMethod cmdlet.
So I try to run:
$Headers = @{
'x-api-key' = 'KEY'
'client_id' = 'VALID_ID'
'client_secret' = 'SECRET'
}
$Parameters = @{
Uri = 'VALID_URI'
Method = 'Post'
Headers = $Headers
ContentType = 'application/json'
}
Invoke-RestMethod @Parameters
This give the following output:
Invoke-RestMethod : {"error":"Invalid user . Can't find credentials."}
At line:13 char:1
+ Invoke-RestMethod @Parameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I have tried a few different variations on this as well as changing up the quotes to no avail. I have a suspicion that the secret which contains "==" is getting chopped up for some reason but using single quotes should have negated that as PowerShell should interpret anything in single quotes as a literal right?
Upvotes: 1
Views: 508
Reputation: 11
Invoke-RestMethod -Method Post -Uri "VALID_URI" -Headers @{"Content-Type" = "application/json"; "x-api-key" = $API_KEY} -Body (ConvertTo-Json @{"client_id" = "VALID_ID"; "client_secret" = "SECRET"})
This is what ended up working. I wasn't too far off and thank you everyone for helping!
Upvotes: 0
Reputation: 31
To mimick the curl command listed:
curl -X POST "https://valid_URI" -H "Content-Type: application/json" -H "x-api-key:KEY" -d "{"client_id":"VALID_ID","client_secret":"SECRET"}"
You would need to include the client_id and client_secret in the body rather than the Header.
$Headers = @{
'x-api-key' = 'KEY'
}
$Body = @{
'client_id' = 'VALID_ID'
'client_secret' = 'SECRET'
} | ConvertTo-Json
$Parameters = @{
Uri = 'VALID_URI'
Method = 'Post'
Headers = $Headers
Body = $Body
ContentType = 'application/json'
}
Invoke-RestMethod @Parameters
Upvotes: 1