繁体   English   中英

我如何将curl转换为powershell

[英]How do i convert curl to powershell

我如何将此curl命令转换为powershell? 这完全使用卷曲,但在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

}

非常感谢

是否有可能首先需要获取令牌,然后需要转换为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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM