如何将curl命令转换为Powershell?这可以完美地使用curl,但是在powershell中,我得到401的未授权。 我已经尝试了所有我能想到的。此代码只能在特定环境中执行。我正在尝试将我知道有效的cookie传递给第二个标题。第一个请求运行良好,第二个请求意味着返回json无效。而是返回401
该命令要求使用cookie进行身份验证
curl.exe -vu SuperGabriel:SuperGabriel@2019 -X POST -H "X-Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login --insecure
curl.exe -v -H "Cookie: thirdParty_SESSIONID=6483556424564819468" -H "X-
Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/3rdParty/AdmtiveDomain --insecure
curl.exe -v -H "Cookie: thirdParty_SESSIONID=1312545750448673312" -H "X-Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/3rdParty/CallRecord/0.106.?day=20190301 -k --insecure -o
"C:\Users\stephenm\Documents\test.csv"
到目前为止,这是我的powershell代码。
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object
TrustAllCertsPolicy
$user = "SuperGabriel"
$pass = "SuperGabriel@2019"
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = @{
"Authorization" = $basicAuthValue
"X-Application" = "3rdParty"
}
$login = Invoke-WebRequest -Uri https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login -Headers $headers -Method Post
$headers = @{
"Authorization" = $basicAuthValue
"X-Application" = "3rdParty"
"Cookie" ="thirdParty_SESSIONID=4436753218874838616"
"Content-Type" = "application/json"
}
try
{
$admtiveDomains = Invoke-WebRequest -Uri https://webadmin.td90.centile-
dev.com/restletrouter/v1/3rdParty/AdmtiveDomain -Headers $headers -Method
Get
}catch{
echo $ErrorMessage = $_.Exception.Message
}
非常感谢
答案 0 :(得分:0)
是否可能需要先获取令牌,然后再将其转换为base64字符串?
$uri = 'https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login'
$body = @{ username = 'SuperGabriel'; password = 'SuperGabriel@2019' }
$bodyjson = $body | convertTo-Json
$ctype = "application/json"
$token = invoke-restmethod -uri $uri -method POST -body $bodyjson -contenttype $ctype
我不知道$token
将返回什么对象,但是从本质上讲,您将转换返回的字符串并从那里继续。
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))