我有原始的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),但它们都失败了,我没有想法。
-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)