PowerShell 的 Invoke-RestMethod 等效于 curl -u(基本身份验证) [英] PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)

查看:25
本文介绍了PowerShell 的 Invoke-RestMethod 等效于 curl -u(基本身份验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当于什么

curl -u username:password ...

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

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 ...

但它返回 401, Unauthorized.

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.

这篇关于PowerShell 的 Invoke-RestMethod 等效于 curl -u(基本身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆