简体   繁体   English

我如何将curl转换为powershell

[英]How do i convert curl to powershell

How do i convert this curl command to powershell? 我如何将此curl命令转换为powershell? This works perfectly using curl however in powershell i am geting 401 unauthorized. 这完全使用卷曲,但在powershell我正在未经授权的401。 I have tried everything i can think of. 我已经尝试了我能想到的一切。 This code can only be executed inside a certain environment. 此代码只能在特定环境中执行。 i am trying to pass a cookie i know is valid into the header of the second. 我试图传递一个我知道有效的cookie到第二个的标题。 The first request works well, the second request which is meant the return json does not. 第一个请求运行良好,第二个请求意味着返回json没有。 Instead returns 401 而是返回401

The command requires cookies to authenitcate 该命令需要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"

Here is my powershell code so far. 到目前为止,这是我的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

}

many thanks 非常感谢

Is it possible you need to get a token first that you then need to convert to a base64 string? 是否有可能首先需要获取令牌,然后需要转换为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

I don't know what object $token would return, but you would essentially then convert the returned string and continue on from there. 我不知道$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