2012-03-08 22 views
1

SOAP Apiを使用してJiraから添付ファイルを削除する方法を探していましたが、これはネイティブでは不可能であると思われます。 thisの質問に対する回答として提案されているJiraの新しいプラグイン、またはhereのようにこれをサポートする既存のプラグインを再コンパイルしてください。Httpを使用してJira 4.4から添付ファイルを削除する方法

This上記の質問への答えは、私が欲しいものを正確に行うようですが、悲しいかな、私は仕事をすることができません。

JIRAが原因不足しているフォームトークンにこの操作を完了できませんでし欠落

XSRFセキュリティトークン:私が得る応答はそれを示すエラーです。

現在のフォームトークンが期限切れになった可能性があるブラウザのCookieをクリアした可能性があります。新しいフォームトークンが再発行されました。私がAsp.Net MVCのC#を使用していたようあるとして、私は調整だけで、サーバーのURLを持つだけでなく、別の資格情報(のJIRAユーザー)と、その答えからコード、を使用している

とユーザー名/パスワードを使用して、リクエストパラメータとして渡さ:

os_username=jirausername&os_password=xxxxxxx 

を次のように私は現在使用しているコードは次のとおりです。私が使用して終了

public void RemoveAttachment(string issueid, string attachmentid) 
     { 
      using (System.Net.WebClient client = new System.Net.WebClient()) 
      { 
       //Compute jira server base url from WS url 
       string baseUrl = _service.Url.Substring(0, _service.Url.IndexOf("/rpc/")); 

       //Compute complete attachment url 
       string attachmenturl = baseUrl + "/secure/DeleteAttachment.jspa?id=" + 
             issueid + "&deleteAttachmentId=" + attachmentid; 

       client.Credentials = new System.Net.NetworkCredential("jirausername", "xxxxxxx"); 
       string response = client.DownloadString(attachmenturl); 
      } 
    } 
+0

添付ファイルの削除を試みる前にJIRA SOAPログインを実行しましたか?つまり、 'jira_token = jira_object.login(username、password)'です。私は確信していませんが、それはどうにかしてセキュリティトークンを作成するかもしれません。確認するもう一つのこと - あなたは、Webインターフェイスを使用してそのような添付ファイルを削除できますか?いくつかのアクションはJIRA権限スキームを使って制限されていますが、他には管理者権限が必要です.... –

+0

私はすでにログインを実行していましたが、いくつかの方法でトークンを使用しようとしました。私はまた、適切な権限を既に持っています。しかし、あなたのコメントのおかげで、削除確認ページのURLがDeleteAttachmentであることがわかりました!default.jspa?atl_token = BSMT-K4U9-TEZ2-OYEI | a1427bcf5fd94ffb96c4634055a53a6c00af8b00 | lin&id = 10598&deleteAttachmentId = 10602(!default.jspaの部分とトークン)私を解決に導いた。詳細については、以下の回答を参照してください。どのバージョンを使用していますか?私はatl_tokenが比較的新しいと思います... – rusmus

+0

私はv4.0.1#471を使用しています。それを並べ替えておめでとう。 –

答えて

0

最初に削除確認フォームを要求し、次にフォームから必要なトークンを抽出し、最後に添付ファイルを削除するためにフォームコンテンツに相当するものを投稿するメソッドです。以下のコード。

public void RemoveAttachment(string issueid, string attachmentid) 
{ 
    //Compute jira server base url from WS url 
    string baseUrl = _service.Url.Substring(0, _service.Url.IndexOf("/rpc/")); 

    //Compute complete attachment deletion confirm url 
    string confirmurl = baseUrl + "/secure/DeleteAttachment!default.jspa?id=" + 
       issueid + "&deleteAttachmentId=" + attachmentid + "&os_username=jirauser&os_password=xxxxxx"; 

    //Create a cookie container to maintain the xsrf security token cookie. 
    CookieContainer jiracontainer = new CookieContainer(); 

    //Create a get request for the page containing the delete confirmation. 
    HttpWebRequest confirmrequest = (HttpWebRequest)WebRequest.Create(confirmurl); 
    confirmrequest.Credentials = System.Net.CredentialCache.DefaultCredentials; 
    confirmrequest.CookieContainer = jiracontainer; 

    //Get the response and the responsestream. 
    WebResponse confirmdeleteresponse = confirmrequest.GetResponse(); 
    Stream ReceiveStream = confirmdeleteresponse.GetResponseStream(); 

    // Open the stream using a StreamReader for easy access. 
    StreamReader confirmreader = new StreamReader(ReceiveStream); 
    // Read the content. 
    string confirmresponse = confirmreader.ReadToEnd(); 

    //Create a regex to extract the atl/xsrf token from a hidden field. (Might be nicer to read it from a cookie, which should also be possible). 
    Regex atl_token_matcher = new Regex("<input[^>]*id=\"atl_token\"[^>]*value=\"(?<token>\\S+)\"[^>]*>", RegexOptions.Singleline); 
    Match token_match = atl_token_matcher.Match(confirmresponse); 

    if (token_match.Success) 
    { 
     //If we found the token get the value. 
     string token = token_match.Groups["token"].Value; 

     //Compute attachment delete url. 
     string deleteurl = baseUrl + "/secure/DeleteAttachment.jspa"; 

     //Construct form data. 
     string postdata = "atl_token=" + HttpContext.Current.Server.UrlEncode(token) + "&id=" + issueid + "&deleteAttachmentId=" + attachmentid + "&Delete=Delete&os_username=jirauser&os_password=xxxxxx"; 

     //Create a post request for the deletion page. 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(deleteurl); 
     request.KeepAlive = false; 
     request.CookieContainer = jiracontainer; // Remember to set the cookiecontainer. 
     request.ProtocolVersion = HttpVersion.Version10; 
     request.Method = "POST"; 

     //Turn our request string into a byte stream 
     byte[] postBytes = Encoding.ASCII.GetBytes(postdata); 

     //Make sure you specify the proper type. 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = postBytes.Length; 
     Stream requestStream = request.GetRequestStream(); 

     //Send the post. 
     requestStream.Write(postBytes, 0, postBytes.Length); 
     requestStream.Close(); 

     //Get the response. 
     WebResponse deleteresponse = request.GetResponse(); 

     // Open the responsestream using a StreamReader for easy access. 
     StreamReader deleteresponsereader = new StreamReader(deleteresponse.GetResponseStream()); 

     // Read the content. 
     string deleteresponsecontent = deleteresponsereader.ReadToEnd(); 

     // do whatever validation/reporting with the response... 
    } 
    else 
    { 
     //We couldn't find the atl_token. Throw an error or something... 
    } 

} 

編集: 同じことがコメントを削除するために機能します。 'attachment'を 'comment'に、 'deleteAttachmentId'を 'commentId'に置き換えてください。あなたはうまくいくはずです。

関連する問題