JIRA REST API使用PowerShell v3提供HTTP错误400

时间:2013-08-21 22:06:38

标签: rest powershell jira

我正在尝试编写一些PowerShell函数来与我们的Atlassian JIRA系统(JIRA 5.2,下载版本)进行交互。不幸的是,我通过反复试验发现Invoke-RestMethod似乎不起作用(不支持身份验证标头),所以我写了一个名为Invoke-JiraMethod的简单函数。我可以确认这个方法适用于GET请求;我已经能够用它来让Jira对象满足我的心愿。但是,一旦我尝试创建问题,我就开始收到HTTP 400 /错误请求错误。

我已按照here步骤获取问题元数据,并填写输入对象中的所有必填字段。任何人都可以帮我弄清楚如何解决400错误?如果需要,我可以提供更多信息 - 我只是不想溢出问题的描述。 :)

Function Invoke-JiraMethod
{
    <#
    .Synopsis
        Low-level function that directly invokes a REST method on JIRA
    .Description
        Low-level function that directly invokes a REST method on JIRA.  This is designed for 
        internal use.
    #>
    [CmdletBinding()]
    param
    (
        [ValidateSet("Get","Post")] [String] $Method,
        [String] $URI,
        [String] $Username,
        [String] $Password
    )

    process
    {
        $token = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${Username}:${Password}"))

        $webRequest = [System.Net.WebRequest]::Create($URI)
        $webRequest.Method = $Method.ToUpper()

        $webRequest.AuthenticationLevel = "None"
        $webRequest.Headers.Add('Authorization', "Basic $token")
        #$webRequest.Headers.Add('Authorization', $token)
        $webRequest.PreAuthenticate = $true
        $webRequest.ContentType = "application/json"

        Write-Verbose "Invoking JIRA method $Method with URI $URI"
        $response = $webRequest.GetResponse()
        $requestStream = $response.GetResponseStream()
        $readStream = New-Object -TypeName System.IO.StreamReader -ArgumentList $requestStream
        $json = $readStream.ReadToEnd()
        $readStream.Close()
        $response.Close()

        $result = $json | ConvertFrom-Json

        Write-Output $result
    }
}

Function New-JiraIssue
{
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 0)]
        [string] $ProjectKey,

        [Parameter(Mandatory = $true,
                   Position = 1)]
        [string] $IssueType,

        [Parameter(Mandatory = $false)]
        [string] $Priority = 3,

        [Parameter(Mandatory = $true,
                   Position = 2)]
        [string] $Summary,

        [Parameter(Mandatory = $true,
                   Position = 3)]
        [string] $Description,

        [Parameter(Mandatory = $true,
                   Position = 4)]
        [string] $Location,

        [Parameter(Mandatory = $true,
                   Position = 5)]
        [string] $Phone,

        [Parameter(Mandatory = $false)]
        [string] $Reporter,

        [Parameter(Mandatory = $false)]
        [PSCredential] $Credential
    )

    process
    {
        $ProjectObject = New-Object -TypeName PSObject -Property @{"key"=$ProjectKey}
        $IssueTypeObject = New-Object -TypeName PSObject -Property @{"id"=$IssueType}

        if ( -not ($Reporter))
        {
            Write-Verbose "Reporter not specified; defaulting to $JiraDefaultUser"
            $Reporter = $JiraDefaultUser
        }

        $ReporterObject = New-Object -TypeName PSObject -Property @{"name"=$Reporter}

        $fields = New-Object -TypeName PSObject -Property ([ordered]@{
            "project"=$ProjectObject;
            "summary"=$Summary;
            "description"=$Description;
            "issuetype"=$IssueTypeObject;
            "priority"=$Priority;
            "reporter"=$ReporterObject;
            "labels"="";
            $CustomFields["Location"]=$Location;
            $CustomFields["Phone"]=$Phone;
            })


        $json = New-Object -TypeName PSObject -Property (@{"fields"=$fields}) | ConvertTo-Json

        Write-Verbose "Created JSON object:`n$json"

        # https://muwebapps.millikin.edu/jira/rest/api/latest/issue/IT-2806
        # $result = Invoke-RestMethod -Uri $JiraURLIssue -Method Post -ContentType "application/json" -Body $json -Credential $Credential

        if ($Username -or $Password)
        {
            $result = (Invoke-JiraMethod -Method Post -URI "${JiraURLIssue}" -Username $Username -Password $Password)
        } else {
            $result = (Invoke-JiraMethod -Method Post -URI "${JiraURLIssue}" -Username $JiraDefaultUser -Password $JiraDefaultPassword)
        }

        Write-Output $result
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我收到错误400,因为我放入json数据的问题类型ID号未映射到任何内容。提琴手帮我诊断了。

我使用这段代码通过invoke-restmethod&#39; s -Headers选项找出对jira的身份验证:http://poshcode.org/3461

然后我将json数据放入here字符串,然后运行invoke-restmethod。

下面的代码位。替换&#34;任何有效的...类型ID号&#34;您可以从jira admin获得实际项目和问题类型ID。

$uri = "$BaseURI/rest/api/2/issue" 


    $jsonString = @'
{
    "fields": {
       "project":
       {
          "id": "any valid project id number"
       },
       "summary": "No REST for the Wicked.",
       "description": "Creating of an issue using ids for projects and issue types using the REST API",
       "issuetype": {
          "id": "any valid issue type id number"
       }
   }
}  
'@  


        $headers = Get-HttpBasicHeader $Credentials

        Invoke-RestMethod -uri $uri -Headers $headers  -Method Post -Body $jsonString -ContentType "application/json"