通过 REST 创建 Jira 用户导致 401 - 此资源需要 WebSudo

Jira user creation via REST results in 401 - This resource requires WebSudo

我正在尝试编写一个 PowerShell 脚本来自动执行将新用户帐户添加到我们的 Jira 实例的过程。我已经提供了我的代码,但老实说,我什至没有做到这一点,因为我收到了 401 错误:

This resource requires WebSudo.

我在 Jira 支持论坛上看到了这两篇文章,但我不清楚如何调整代码以获取并将其应用到我的 REST 调用中。如果这样可以使所有这一切变得更容易,我可以将其更改为使用 .Net WebClient class,但现在我有点不知所措。

$url = "https://devjira.domain.com/rest/api/2/user"


$user = "admin"
$pass = "super secure password"
$secpasswd = ConvertTo-SecureString $user -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($pass, $secpasswd);

$userObject = @{
    name     = "rkaucher@domain.net";
    emailAddress = "robert_kaucher@domain.com";
    displayName  = "Bob Kaucher";
    notification = $true;
}

$restParameters = @{
    Uri = $url;
    ContentType = "application/json";
    Method = "POST";
    Body = (ConvertTo-Json $userObject).ToString();
    Credential = $cred;

}

Invoke-RestMethod @restParameters

JSON输出

{
    "name":  "rkaucher@domain.net",
    "displayName":  "Bob Kaucher",
    "emailAddress":  "robert_kaucher@domain.com",
    "notification":  true
}

我将脚本的身份验证组件更改为:

$cred = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$user`:$pass"))
$headers = @{Authorization=("Basic $cred")} 

这是基于以下问题的选定答案:

PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)

方法的最终调用如下所示:

$restParameters = @{
    Uri = $url;
    ContentType = "application/json";
    Method = "POST";
    Body = (ConvertTo-Json $userObject).ToString();
    Headers = $headers;
}

$response = Invoke-RestMethod @restParameters