Can';t translate curl调用PowerShell中的WebRequest(-unsecure/-k未找到)
我有一个原始的curl调用,据说它是在Unix环境中工作的(或者在提供者的办公室使用的任何东西) 使用,我将标志和属性交换到以下内容Can';t translate curl调用PowerShell中的WebRequest(-unsecure/-k未找到),powershell,curl,Powershell,Curl,我有一个原始的curl调用,据说它是在Unix环境中工作的(或者在提供者的办公室使用的任何东西) 使用,我将标志和属性交换到以下内容 Invoke-WebRequest -User ybeepbeepbeepa:eboopboopboopa -Method POST -Headers @{"Content-Type"="application/x-www-form-urlencoded"} -Uri "https://xxx/oauth2/token?grant_type
Invoke-WebRequest
-User ybeepbeepbeepa:eboopboopboopa
-Method POST
-Headers @{"Content-Type"="application/x-www-form-urlencoded"}
-Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid"
我唯一没有翻译的部分是-k,它应该相当于-不安全。通过查看上述文档,我找到了一些可能的、虽然牵强的替代方案(如-AllowUnencryptedAuthentication),但都失败了,我也没有主意了
代替
-k
,您需要使用ServicePointManager
类为应用程序域设置证书验证例程:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
对于-u
标志,您需要:
function Get-BasicAuthCreds {
param([string]$Username,[string]$Password)
$AuthString = "{0}:{1}" -f $Username,$Password
$AuthBytes = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
return [Convert]::ToBase64String($AuthBytes)
}
$Headers = @{"Content-Type"="application/x-www-form-urlencoded"}
$Headers['Authorization'] = "Basic $(Get-BasicAuthCreds ybeepbeepbeepa eboopboopboopa)"
Invoke-WebRequest -Method POST -Headers $Headers -Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid"
如果要内联生成凭证字符串,可以这样做(尽管它有点笨重):
当您拥有自签名证书并且使用5.x版或更低版本的powershell时,前一篇文章应该可以帮助您使用-k:是否可以将其内联表达而不是函数?太棒了。不过,仍将有两行执行。我希望有一个内联的和一行,我可以发送给我的同事作为一个神奇的药丸(你知道-粘贴在这个,并按下回车键,没有思考)。不过,我认为,只要我们能够在PowerShell中执行变量声明,这就足够了。(例如,访问$PROFILE文件时,我们受到了极大的限制。)
-替换“\r?\n”,;”代码>;-)
function Get-BasicAuthCreds {
param([string]$Username,[string]$Password)
$AuthString = "{0}:{1}" -f $Username,$Password
$AuthBytes = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
return [Convert]::ToBase64String($AuthBytes)
}
$Headers = @{"Content-Type"="application/x-www-form-urlencoded"}
$Headers['Authorization'] = "Basic $(Get-BasicAuthCreds ybeepbeepbeepa eboopboopboopa)"
Invoke-WebRequest -Method POST -Headers $Headers -Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid"
$Headers = @{
"Content-Type" = "application/x-www-form-urlencoded"}
"Authorization" = "Basic $([Convert]::ToBase64String([System.Text.Encoding]::Ascii.GetBytes('ybeepbeepbeepa:eboopboopboopa')))"
}