Powershell 调用RestMethod还是调用curl的WebRequest版本?

Powershell 调用RestMethod还是调用curl的WebRequest版本?,powershell,curl,webrequest,Powershell,Curl,Webrequest,我正在尝试访问本地服务器上Sonarqube的web api。Sonarqube文档中的示例都是基于curl的,我试图将它们转换为PowerShell,要么调用WebRequest,要么调用RestMethod。我的证件有问题。我一直收到一个401未授权错误。我已经阅读了每一篇我能找到的关于如何做到这一点的帖子,但我没有看到一个全面完整的答案 以下是我正在运行的内容: $user='myUserid' $password='myPassword' $secpasswd = ConvertTo-S

我正在尝试访问本地服务器上Sonarqube的web api。Sonarqube文档中的示例都是基于curl的,我试图将它们转换为PowerShell,要么调用WebRequest,要么调用RestMethod。我的证件有问题。我一直收到一个401未授权错误。我已经阅读了每一篇我能找到的关于如何做到这一点的帖子,但我没有看到一个全面完整的答案

以下是我正在运行的内容:

$user='myUserid'
$password='myPassword'
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)
$uri="http://myServer.ad1.prod:9000/api/properties"
Invoke-RestMethod -Uri $uri -Credential $cred -Method Get
答复是:

Invoke-RestMethod : {"err_code":401,"err_msg":"Unauthorized"}
我已经按照原始产品文档中的定义运行了curl命令,它可以正常工作。相同的用户ID、相同的密码、相同的URI、相同的方法

有人能提供建议吗

我的curl命令是:

curl -u myID:myPassword X GET http://myServer.ad.prod:9000/api/properties

我认为您需要通过这样的凭证:

$username = "user"
$password = "password"
$authInfo = ("{0}:{1}" -f $username,$password)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}

Invoke-RestMethod -Method Head -Uri "https://pathtowhatever" -ContentType 'text/json' -Headers $headers

我认为您需要通过这样的凭证:

$username = "user"
$password = "password"
$authInfo = ("{0}:{1}" -f $username,$password)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}

Invoke-RestMethod -Method Head -Uri "https://pathtowhatever" -ContentType 'text/json' -Headers $headers

如何使用
curl
?@AnsgarWiechers传递凭据-请参阅编辑的帖子如何使用
curl
?@AnsgarWiechers传递凭据-请参阅编辑的帖子。谢谢,成功了。谢谢