Robert Kaucher
Robert Kaucher

Reputation: 1861

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

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     = "[email protected]";
    emailAddress = "[email protected]";
    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":  "[email protected]",
    "displayName":  "Bob Kaucher",
    "emailAddress":  "[email protected]",
    "notification":  true
}

Upvotes: 2

Views: 2684

Answers (1)

Robert Kaucher
Robert Kaucher

Reputation: 1861

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

Upvotes: 2

Related Questions