PowerShell的Invoke-RestMethod相当于curl -u(基本身份验证)

Bor*_*ard 54 powershell curl basic-authentication

相当于什么

curl -u username:password ...
Run Code Online (Sandbox Code Playgroud)

在PowerShell中Invoke-RestMethod?我试过这个:

$securePwd = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePwd)

Invoke-RestMethod -Credential $credential ...
Run Code Online (Sandbox Code Playgroud)

但它返回401,未经授权.

Bor*_*ard 91

到目前为止,这是唯一对我有用的方法:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ...
Run Code Online (Sandbox Code Playgroud)

但我不相信没有更好的方法.


Ryn*_*ant 15

我不确定为什么-Credential参数在你的情况下不起作用,但它适用于httpbin服务.

你可以试试这个:

$pwd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('PsUser', $pwd)

Invoke-RestMethod 'http://httpbin.org/basic-auth/PsUser/MyPassword' -cred $cred
Run Code Online (Sandbox Code Playgroud)

编辑:如评论中所述,此方法不会在初始请求上发送Authorization标头.它等待质询响应,然后使用Authorization标头重新发送请求.这对于初始请求中需要凭据的服务不起作用.

  • @Borek在运行WireShark时检查httpbin.org后,我发现使用`-Credential`不会在第一个请求中添加Authorization标头.Httpbin.org以401响应,然后PowerShell发送第二个具有Authorization标头的请求.Httpbin.org在第二次请求后以200响应.据我所知,您的方法是在第一个请求上发送标头的唯一方法. (2认同)

Jaq*_*nek 9

#Requires -Version 6
$Uri = 'https://httpbin.org/basic-auth/user/pass'
# use "user" and "pass" when prompted for credentials
$Credential = Get-Credential
Invoke-RestMethod -Uri $Uri -Authentication Basic -Credential $Credential
Run Code Online (Sandbox Code Playgroud)

  • 这里有很多非常古老的答案,但这是正确的现代答案。提供凭证对象并指定“-Authentication Basic”,以便在 PowerShell 中轻松进行基本身份验证。 (2认同)

Mon*_*ton 8

看来你应该在它们独立失败时组合方法.

创建凭据并将其添加到请求中.

创建标头并将其添加到请求中.

$username = "username";
$password = ConvertTo-SecureString –String "password" –AsPlainText -Force
$credential = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $username, $password

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$getProjectUri = "yourUri"
Invoke-RestMethod -Method Get -Uri $getProjectUri -Headers @{Authorization = "Basic $base64AuthInfo" } -Credential $credential -ContentType "application/json"
Run Code Online (Sandbox Code Playgroud)


Jas*_*hie 6

此版本适用于Get-CredentialPSCredential对象。它还可以在 PowerShell 6.0 中跨平台工作。它通过避免使用 BSTR 调用来做到这一点,有时在尝试从PSCredential.

$creds = Get-Credential
$unsecureCreds = $creds.GetNetworkCredential()
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $unsecureCreds.UserName,$unsecureCreds.Password)))
Remove-Variable unsecureCreds

Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ...
Run Code Online (Sandbox Code Playgroud)


小智 5

我发现,-WebSession如果您使用凭据预先创建一个 WebRequestSession 对象,那么使用该参数是有效的。我不会重复如何创建 PS Credential 对象,因为其他答案中已经介绍了这一点。

$WebSession = New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession -Property @{Credentials=$Credential}
Invoke-RestMethod -Uri "your_URI" -WebSession $WebSession
Run Code Online (Sandbox Code Playgroud)

此方法在第一次调用时发送 auth 标头,因此避免了 401 响应。

顺便说一句,这种方法还可以用于设置代理详细信息(当使用参数指定时,代理详细信息在所有版本的 PS 中都不能正常工作),并在您的 API 需要时处理 cookie。


小智 5

您基本上需要将用户名和密码对Invoke-RestMethod作为编码的凭据变量传递。

对我有用的是以下内容:

$USERNAME = 'user'
$PASSWORD = 'password'
$IDP_URL = 'example.com/token'


$credPair = "$($USERNAME):$($PASSWORD)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))

$parameters = @{
    Uri         = $IDP_URL
    Headers     = @{ 'Authorization' = "Basic $encodedCredentials" }
    Method      = 'POST'
    Body        = '...'
    ContentType = '...'
}

Invoke-RestMethod @parameters
Run Code Online (Sandbox Code Playgroud)

请注意如何提取请求参数$parameters以避免命令臃肿。