通过REST生成Jira用户的结果为401-此资源需要WebSudo [英] Jira user creation via REST results in 401 - This resource requires WebSudo
问题描述
我正在尝试编写一个PowerShell脚本,该脚本将自动执行将新用户帐户添加到我们的Jira实例的过程.我已经提供了我的代码,但老实说,由于我收到401错误,我什至没有做到这一点:
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:
此资源需要WebSudo.
This resource requires WebSudo.
我在Jira支持论坛上看到了这两篇文章,但是我不清楚如何修改代码,然后将其应用于REST调用.如果可以简化所有操作,我可以更改为使用.Net WebClient类,但是现在我有点茫然了.
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输出
{
"name": "rkaucher@domain.net",
"displayName": "Bob Kaucher",
"emailAddress": "robert_kaucher@domain.com",
"notification": true
}
推荐答案
我将脚本的身份验证组件更改为:
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的Invoke-RestMethod等效于curl -u(基本身份验证)
该方法的最终调用如下:
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
这篇关于通过REST生成Jira用户的结果为401-此资源需要WebSudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!