2015-09-25 65 views
7

我正在從Confluence的SOAP API遷移到使用their REST API。我看到有支持for adding attachments to a page (by doing a POST)但我遇到的問題得到它的工作(我得到一個403:禁止錯誤消息)。我有其他「get」項通過其餘api正常工作,但做一個附件帖子似乎仍然失敗。在C#中,將附件張貼到Confluence REST API的正確方法是什麼?

這裏是我當前的代碼(給出了具體的文件名):

byte[] rawData = File.ReadAllBytes(filename); 
var pageId = "11134"; 
var url = new Uri("http://example.com:9088/rest/api/content/" + pageId + "/child/attachment"); 
var requestContent = new MultipartFormDataContent(); 
var imageContent = new ByteArrayContent(rawData); 
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(attachement.contentType); 
requestContent.Add(imageContent, "file", attachement.fileName); 
requestContent.Headers.Add("X-Atlassian-Token", "nocheck"); 

你能看到,如果我在做什麼上面了?

+0

合流文檔留下:「返回如果附件被禁用,或者如果你沒有權限將附件添加到這些內容。」您是否嘗試過使用CURL,如https://docs.atlassian.com/confluence/REST/latest/#d3e787中的頁面,看看它是否仍然返回403? –

+0

我只是嘗試捲曲,我得到一個錯誤捲曲<56> Recv失敗:連接被重置。我使用了這個語法curl -D- -u user:pwd -X POST -H「X-Atlassian-Token:nocheck」-F「[email protected]」htttp://例子:9088/rest/api/content/71105117 /孩子/附件(注意:我在這個評論中的「http」中加了一個額外的「t」,所以SO不會嘗試將它轉換爲超鏈接 – leora

+0

@SimonMourier - 還有,C#代碼還需要其他身份驗證??以上 – leora

回答

3

從合流文檔(RTFM)

In order to protect against XSRF attacks, because this method accepts multipart/form-data, it has XSRF protection on it. This means you must submit a header of X-Atlassian-Token: nocheck with the request, otherwise it will be blocked.

Post

httpClient.Headers.Add("X-Atlassian-Token", "nocheck"); 
+0

我已添加新行並更新了問題。現在的問題是,嘗試添加這個,現在我得到403:Forbidden – leora

+6

SO規定每個職位只有1個問題。更換新的問題應該是另一篇文章。原因是其他人能夠搜索特定問題的答案。 –

4

403狀態之前添加這表明請求未被授權。爲了授權請求,您需要指定Authorization標頭。 Confluence REST API支持基本授權方案。對於基本身份驗證,您需要爲每個請求指定以下標題:Authorization: Basic username:password其中用戶名:密碼部分應該是Base64編碼的。您可以使用下面的代碼,以做到這一點:

string userName; 
string password; 
string authorizationString = userName + ":" + password; 
string encodedValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(authorizationString)); 
string authorizationHeaderValue = "Basic " + encodedValue; 

requestContent.Headers.Add("Authorization", authorizationHeaderValue); 

根據這一link你還應該指定每個請求以下網址參數:os_authType=basic

HTTP basic authentication: (Authorization HTTP header) containing 'Basic username:password'. Please note however, username:password must be base64 encoded. The URL must also contain the 'os_authType=basic' query parameter.

注意:如果使用基本認證,請確保通過https進行連接;

0

這是我喜歡的方式:

string url = "https://localhost:8080/confluence/rest/api/content/123456/child/attachment"; 
string filename = @"C:\temp\test.txt"; 
using (var client = new WebClient()) 
{ 
    string authorizationString = username + ":" + password; 
    string encodedValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(authorizationString)); 
    client.Headers.Add("Authorization", "Basic " + encodedValue); 
    client.Headers.Add("X-Atlassian-Token", "nocheck"); 
    byte[] result = client.UploadFile(url, filename); 
    string responseAsString = Encoding.Default.GetString(result); 
}