我在将curl命令转换为Powershell Invoke-RestMethod时遇到问题。我已经尝试了许多不同的方法,但我得到了错误消息: Invoke-RestMethod : The remote server returned an error:(401) Unauthorized。
我需要为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我得到了两个相同的错误
如果有人能帮上忙我将不胜感激
发布于 2021-11-25 08:52:13
多亏了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_tokenhttps://stackoverflow.com/questions/70107914
复制相似问题