2012-03-22 87 views
1

我試圖上傳文件併發布一些消息給Yammer。發佈消息有效(所有授權內容等都可以)。當我想要附加文件時出現問題。 我試圖按照這裏的代碼:Yammer.NetYammer API - 附件上傳

但我不履行我缺少一些部分(無法引用整個項目,不知何故我的Visual Studio有問題)。

這就是爲什麼我試圖遵循指定請求參數的傳統方式。下面我把我的方法:

public bool postMessage(string body, string attachement) 
    { 
     var url = "https://www.yammer.com/api/v1/messages.json"; 
     NameValueCollection parameters = new NameValueCollection(); 
     parameters.Add("body", body); 
     parameters.Add("group_id", group_id); 

     var authzHeader = oauth.GenerateAuthzHeader(url, "POST"); 
     //new request 
     var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); 
     request.Headers.Add("Authorization", authzHeader); 
     request.Method = "POST"; 
     //content type settings 
     request.ContentType = "multipart/form-data"; 
     request.KeepAlive = true; 
     //Proxy settings 
     request.Proxy = new System.Net.WebProxy("my company's proxy", true); 
     request.Proxy.Credentials = new System.Net.NetworkCredential(user, password); 

     //prepare the parameters 

     FileInfo fi = new FileInfo(attachement); 
     int i = 0; 
     long postDataSize = fi.Length;   

     parameters.Add("attachment", "attachment1"); 
     parameters.Add("file", Path.GetFileName(attachement)); 

     int count = 0; 
     string wdata = string.Empty; 
     foreach (string key in parameters.Keys) 
     { 
      if (count == 0) 
      { 
       wdata = key + "=" + oauth.encode(parameters[key]); 
      } 
      else 
      { 
       wdata += "&" + key + "=" + oauth.encode(parameters[key]); 
      } 
      count++; 
     } 

     //add the parameters 
     byte[] postDataBytes = Encoding.ASCII.GetBytes(wdata); 
     request.ContentLength = postDataBytes.Length + postDataSize; 
     Stream reqStream = request.GetRequestStream(); 
     reqStream.Write(postDataBytes, 0, postDataBytes.Length); 


     //write the file 
     //postDataBytes = Encoding.ASCII.GetBytes(fileHeader); 
     //reqStream.Write(postDataBytes, 0, postDataBytes.Length); 

     int bufferSize = 10240; 
     FileStream readIn = new FileStream(attachement, FileMode.Open, FileAccess.Read); 
     readIn.Seek(0, SeekOrigin.Begin); // move to the start of the file 
     byte[] fileData = new byte[bufferSize]; 
     int bytes; 
     while ((bytes = readIn.Read(fileData, 0, bufferSize)) > 0) 
     { 
      // read the file data and send a chunk at a time 
      reqStream.Write(fileData, 0, bytes); 
     } 
     readIn.Close(); 

     reqStream.Close(); 

     using (var response = (System.Net.HttpWebResponse)request.GetResponse()) 
     { 
      using (var reader = new System.IO.StreamReader(response.GetResponseStream())) 
      { 
       string resp = reader.ReadToEnd(); 

       return true; 
      } 
     } 
    } 

該代碼可能看起來有點混亂,但我希望你明白了。 主要問題是:

  • 當我只發佈一條消息(group_id,body)它的工作原理。
  • 當我嘗試上述方法併發布附件時,我收到Yammer的「內部服務器錯誤」。

有誰知道如何使用API​​將文件上傳到Yammer?可以在.NET中使用:)

+1

煮沸即可下來到不工作的最簡單的事情嗎? – 2012-03-22 18:15:48

+0

添加關於文件流的部分是我認爲的問題的根源。我假設它在Yammer方面產生了內部服務器錯誤,所以我想我以某種方式(顯然是?)做錯了。 – Matt 2012-03-23 13:11:47

+0

好吧,我發現了一個可能的解決方案[這裏](http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data) - 我不必要地堅持Yammer API的主題,而問題是請求代碼本身,而不是API – Matt 2012-03-26 12:54:46

回答

0

使用MultipartFormDataContent類型可以將二進制內容發佈到Yammer messages.json REST端點。

工作示例發佈了一些圖片,文字和標籤:

WebProxy proxy = new WebProxy() 
{ 
    UseDefaultCredentials = true, 
}; 
HttpClientHandler httpClientHandler = new HttpClientHandler() 
{ 
    Proxy = proxy, 
}; 
using (var client = new HttpClient(httpClientHandler)) 
{ 
    using (var multipartFormDataContent = new MultipartFormDataContent()) 
    { 
    string body = "Text body of message"; 
    var values = new[] 
    { 
     new KeyValuePair<string, string>("body", body), 
     new KeyValuePair<string, string>("group_id", YammerGroupID), 
     new KeyValuePair<string, string>("topic1", "Topic ABC"), 
    }; 
    foreach (var keyValuePair in values) 
    { 
     multipartFormDataContent.Add(new StringContent(keyValuePair.Value), String.Format("\"{0}\"", keyValuePair.Key)); 
    } 
    int i = 1; 
    foreach (Picture p in PictureList) 
    { 
     var FileName = string.Format("{0}.{1}", p.PictureID.ToString("00000000"), "jpg"); 
     var FilePath = Server.MapPath(string.Format("~/images/{0}", FileName)); 
     if (System.IO.File.Exists(FilePath)) 
     { 
      multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(FilePath)), '"' + "attachment" + i.ToString() + '"', '"' + FileName + '"'); 
      i++; 
     } 
    } 
    var requestUri = "https://www.yammer.com/api/v1/messages.json"; 
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); 
    var result = client.PostAsync(requestUri, multipartFormDataContent).Result; 
    } 
}