1

我在将 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

我对他们俩都遇到了同样的错误

如果有人可以提供帮助,我将不胜感激

4

1 回答 1

1

感谢 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
于 2021-11-25T08:52:12.943 回答