更新时间:2022-10-30 11:47:45
为我工作到目前为止:
$ base64AuthInfo = [Convert] :: ToBase64String([Text.Encoding] :: ASCII.GetBytes ({0}:{1}-f $ username,$ password)))
Invoke-RestMethod -Headers @ {Authorization =(Basic {0}-f $ base64AuthInfo) } ...
但我不相信没有更好的办法。 >
What is the equivalent of
curl -u username:password ...
in PowerShell's Invoke-RestMethod
? I tried this:
$securePwd = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePwd)
Invoke-RestMethod -Credential $credential ...
but it returns 401, Unauthorized.
This is the only method that worked for me so far:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ...
But I don't believe there isn't a better way.