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

自古美人都是妖i 提交于 2021-01-28 03:05:41

问题


I'm attempting to write a PowerShell script that will automate the process of adding new user accounts to our Jira instance. I've provided my code but I'm honestly not even getting to that point as I am receiving a 401 error:

This resource requires WebSudo.

I have seen these two posts on the Jira support forum but it's not clear to me how I could adapt the code to get and then apply it to my REST call. I would be fine with changing this to use the .Net WebClient class if that would make all of this easier, but right now I'm at a bit of a loss.

$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 output

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

回答1:


I changed the authentication component of my script to this:

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

This was based on the selected answer to the following:

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

The final invocation of the method looks like this:

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

$response = Invoke-RestMethod @restParameters


来源:https://stackoverflow.com/questions/31819657/jira-user-creation-via-rest-results-in-401-this-resource-requires-websudo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!