无法在 PowerShell 中将curl 转换为 Invoke-WebRequest(--insecure/-k 未找到)

Don*_*ana 3 powershell curl

我有原始的curl调用,据说它在Unix 环境中工作(或者他们在提供商办公室使用的任何环境)。

curl 
  -u ybeepbeepbeepa:eboopboopboopa
  -k 
  -d "grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid" 
  -H "Content-Type:application/x-www-form-urlencoded" 
  https://xxx/oauth2/token
Run Code Online (Sandbox Code Playgroud)

使用curl 的文档,我将标志和属性交换为以下内容。

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" 
Run Code Online (Sandbox Code Playgroud)

我唯一未能翻译的部分是-k,它应该相当于--insecure。检查上述文档,我找到了一些可能的替代方案,尽管有些牵强(例如-AllowUnencryptedAuthentication),但它们都失败了,我没有想法。

  1. PowerShell 的Invoke-WebRequest中的curl 的--insecure(或-k )的等价物是什么(它意外地被添加到curl,由于标志不同,这就像鸭子一样令人困惑)?
  2. 命令的其余部分是否已正确移植到 PowerShell?(我已经签订了一些标志并将它们与 URL 一起烘焙为查询字符串。而且我不完全确定Headers的语法。)

Mat*_*sen 5

-k您需要使用以下类为应用程序域设置证书验证例程来代替ServicePointManager

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
Run Code Online (Sandbox Code Playgroud)

对于该-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"
Run Code Online (Sandbox Code Playgroud)

如果您想内联生成凭证字符串,您可以这样做(尽管它有点笨拙):

$Headers = @{
  "Content-Type"  = "application/x-www-form-urlencoded"} 
  "Authorization" = "Basic $([Convert]::ToBase64String([System.Text.Encoding]::Ascii.GetBytes('ybeepbeepbeepa:eboopboopboopa')))"
}
Run Code Online (Sandbox Code Playgroud)