将curl转换为Powershell Invoke-Restmethod



我在将curl命令转换为Powershell Invoke-RestMethod时遇到问题。我尝试了多种不同的方法,但收到错误消息:Invoke-RestMethod:远程服务器返回错误:(401(未经授权。

我需要为powershell转换的curl命令:

curl -k -d "grant_type=client_credentials" -H "Authorization: Basic sometoken1234567890" https://api.vasttrafik.se:443/token

到目前为止,我已经尝试过:

$token = "c0a6dcae-e14b-3255-88a9-c83df513b314"
$headers = @{Authorization = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("sometoken1234567890"))}
$body = ConvertTo-Json @{"grant_type" = "client_credentials"}

Invoke-RestMethod -Method Post -Headers $headers -Body $body -Uri "https://api.vasttrafik.se:443/token"

还有:

$body = @{
grant_type = "client_credentials"
}
$headers = @{
Authorization = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("sometoken1234567890"))
Accept = "application/json"
ContentType = "application/json"
}
$params = @{
Method = "Post"
Uri = "https://api.vasttrafik.se:443/token"
Body = $body
Header = $headers
}
Invoke-RestMethod @params

我得到了相同的错误,他们两个

如果有人能帮助,我将不胜感激

多亏了Daniel Stenberg,我通过运行以下代码成功地使其工作起来:

$curl = curl.exe -k -d "grant_type=client_credentials" -H "Authorization: Basic sometoken1234567890" api.vasttrafik.se:443/token | ConvertFrom-Json 
$curl.expires_in 
$curl.access_token

最新更新