2012-10-31 45 views
1
上JIRA創建問題

我一直在嘗試與JIRA REST API進行交互(版本= 5.2 m06-4)使用REST API和PowerShell

我有一些代碼如下所示:

function ConvertTo-Base64($string) { 
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($string); 
    $encoded = [System.Convert]::ToBase64String($bytes); 

    return $encoded; 
} 

$reqBody = [System.Text.Encoding]::UTF8.GetBytes($data) 

     try{ 

       $authenticationDetails = ConvertTo-Base64 '$username:$password' 
       Write-Host('Opening a connection to {0}' -f $url) 

       $req = [System.Net.WebRequest]::Create($Url) 
       $req.Headers.Add("AUTHORIZATION", $headerValue); 
       $req.Method = "POST" 
       $req.ContentType = "application/json"   
       $req.Timeout = $Timeout.TotalMilliseconds 

       $req.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 
       $req.PreAuthenticate = $true 

       $req.ContentLength = $reqBody.Length 
       $reqStream = $req.GetRequestStream()    
       $reqStream.Write($reqBody, 0, $reqBody.Length) 
       $reqStream.Close() 

       Write-Host($data) 

       $resp = $req.GetResponse() 
       Write-Verbose $resp 
       Write-Output $resp.StatusCode 
     } 
     catch [System.Net.WebException]{ 
       if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) { 
         $errorResult = $_.Exception.Response.GetResponseStream() 
         $errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd() 
         Write-Warning "The remote server response: $errorText" 
         Write-Output $_.Exception.Response.StatusCode 
         Write-Host $_ 
       } else { 
         throw $_ 
       } 
     } 

代碼通過JSON如下:

$ jsonString ='{「fields」:{「project」:{「key」:「CCB」},「components」:「'+ $ COMPONENT +'」} ,「customfield_11502」:「'+ $ DEPLOYMENT_DATE +'」,「Summary」:「'+ $ description +'」,「issuetype」:{「name」:「Configuration Change」}}}'

我遇到的問題是,當我以這種方式嘗試POST數據時,我被告知摘要,customfield_11502和組件不是屏幕的一部分。當我嘗試刪除JSON的這些屬性時,我被告知我無權發佈問題。

這裏有什麼正確的錯誤信息?是不是我沒有授權,如果是的話,屏幕上的字段不是真的有問題嗎?

當通過REST API傳遞用戶名和密碼時,我使用Base64編碼並將它傳遞給Headers。有什麼明顯的我缺少?

+0

您的授權標頭值不應以'Basic'開始,然後是Base64編碼的用戶名:密碼值。 – codingbadger

回答

0

當使用powershell與任何REST API進行通信時,我強烈建議轉移到powershell v3,以便您不僅可以使用Invoke-RestMethod,還可以使用ConvertTo [From] -Json cmdlet。這就是說,我沒有試圖獲得基本的身份驗證工作。

0

聖牛,這看起來像一個過度複雜的方式來做到這一點。我有大量的JIRA自動化使用powershell,我只是使用cURL(因爲我懶得把它們全部轉換成Invoke-RESTblahblah),我把它作爲一個字符串傳遞給我的URL,並有一個配置文件其他的東西。

作爲一種替代方法,對你有什麼好處嗎?

+1

你能告訴我們一個例子嗎?告訴提問者他們的方式過於複雜(也許!)是一回事,但幫助他們,告訴他們你是如何做到的:-) –

+0

我無法讓代碼正確顯示。下面使用替代方法設置自定義字段: ''{「fields」:{「customfield_10149」:{「id」:「'+ $ projectid +'」}}}'| Out-file -encoding ascii SetProject.json $ URL ='-X PUT --data @ SetProject.json https://myinstance.jira.com/rest/api/latest/issue/'+$Issue cmd/c 「curl.exe $ URL」 刪除項目SetProject.json' 我在許多不同的地方使用這種方法;雖然我沒有使用上面顯示的錯誤陷印(我曾經把它當成問題的次數......一年以上可能少於10次),但我懷疑它可以相對容易地添加。 – Lee

0

使用powershell 3.0 & Jira 6.1.6,我能夠得到這個,http://poshcode.org/5471,代碼工作通過更改https到http並將jira服務器設置爲我們的。

它獲取安全憑證作爲運行功能的一部分。