如何将Pending Attachment上传到Yammer

时间:2013-08-01 12:20:18

标签: httpwebrequest webclient yammer

我正在尝试通过REST Api将文件上传到yammer。

API说,我可以使用

POST https://www.yammer.com/api/v1/messages.json

attachmentn and pending_attachmentn - Yammer provides two methods to associate attachments with a message. Both make use of multi-part HTTP upload (see RFC1867).

然后我尝试通过WebRequest.like link

发布我的消息

但不幸的是,我收到了“内部服务器错误”[500]。像这样question

有人可以告诉我,如何将文件上传到yammer?

以及如何获取pending_attachment列表?

2 个答案:

答案 0 :(得分:0)

好吧,最后,我知道失败的原因。 请求格式。

RFC 1867说:

    Content-type: multipart/form-data, boundary=AaB03x

    --AaB03x
    content-disposition: form-data; name="field1"

    Joe Blow
    --AaB03x
    content-disposition: form-data; name="pics"; filename="file1.txt"
    Content-Type: text/plain

     ... contents of file1.txt ...
    --AaB03x--

有两点

  1. multipart / form-data,boundary = AaB03x - > 的的multipart / form-data的; boundary = AaB03x
  2. 第一行没有空行
  3. Content-type: multipart/form-data, boundary=AaB03x
    --AaB03x
    Content-Disposition: form-data; name="field1"
    
    Joe Blow
    --AaB03x
    Content-Disposition: form-data; name="pics"; filename="file1.txt"
    Content-Type: text/plain
    
    ... contents of file1.txt ...
    --AaB03x--
    

答案 1 :(得分:0)

我坚持了几天才发布带附件的邮件。我不知道如何使用attachmentN发布消息,但它正在使用pending_attachmentN方法。

首先,您必须调用pending_attachment API并获取结果ID。然后在消息API上将ID分配给pending_attachmentN。

我将sample称为我的基线代码。然后我合并了这个C# HttpClient 4.5 multipart/form-data upload

查找以下代码以发布带有Yammer附件的邮件。希望可以节省你的日子。

    public static async Task<PendingAttachment> Upload(string FilePath)
    {
        byte[] byteFile = System.IO.File.ReadAllBytes(FilePath);
        FileInfo fi = new FileInfo(FilePath);

        PendingAttachment pa = null;
        using (var client = new HttpClient())
        {
            string token = "XXXXXXXXXXXXX";
            client.DefaultRequestHeaders.Add("Authorization", "Bearer" + token);
            using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
            {
                content.Add(new StreamContent(new MemoryStream(byteFile)), "attachment", fi.Name);

                using (var message = await client.PostAsync("https://www.yammer.com/api/v1/pending_attachments", content))
                {
                    if (message.IsSuccessStatusCode)
                    {
                        var result = await message.Content.ReadAsStringAsync();
                        pa = Newtonsoft.Json.JsonConvert.DeserializeObject<PendingAttachment>(result);
                        return pa;
                    }
                }
            }
        }
        return null;
    }

    var FilePath = @"D:\Workspace\Lorem ipsum dolor sit amet.docx";
    var pa = await Upload(FilePath);

    MessageParam message = new MessageParam()
    {
            body = "posting attachment",
            group_id = XXXXXXX,
            pending_attachment1 = pa.id
    };
    var result = await CreateMessageAsync(message);

PendingAttachment Class及其属性类

    public class PendingAttachment
    {
        public int id { get; set; }
        public int network_id { get; set; }
        public string url { get; set; }
        public string web_url { get; set; }
        public string type { get; set; }
        public string name { get; set; }
        public string original_name { get; set; }
        public string full_name { get; set; }
        public string description { get; set; }
        public string content_type { get; set; }
        public string content_class { get; set; }
        public string created_at { get; set; }
        public int owner_id { get; set; }
        public bool official { get; set; }
        public string small_icon_url { get; set; }
        public string large_icon_url { get; set; }
        public string download_url { get; set; }
        public string thumbnail_url { get; set; }
        public string preview_url { get; set; }
        public string large_preview_url { get; set; }
        public int size { get; set; }
        public string owner_type { get; set; }
        public string last_uploaded_at { get; set; }
        public int last_uploaded_by_id { get; set; }
        public string last_uploaded_by_type { get; set; }
        public object uuid { get; set; }
        public object transcoded { get; set; }
        public object streaming_url { get; set; }
        public string path { get; set; }
        public int y_id { get; set; }
        public string overlay_url { get; set; }
        public string privacy { get; set; }
        public object group_id { get; set; }
        public bool is_pending { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public string scaled_url { get; set; }
        public Image image { get; set; }
        public int latest_version_id { get; set; }
        public string status { get; set; }
        public Latest_Version latest_version { get; set; }
        public Stats stats { get; set; }
        public string _OriginalFileName { get; set; }
    }

    public class Image
    {
        public string url { get; set; }
        public int size { get; set; }
        public string thumbnail_url { get; set; }
    }

    public class Latest_Version
    {
        public int id { get; set; }
        public int file_id { get; set; }
        public string content_type { get; set; }
        public int size { get; set; }
        public int uploader_id { get; set; }
        public string created_at { get; set; }
        public string path { get; set; }
        public string download_url { get; set; }
        public string thumbnail_url { get; set; }
        public string preview_url { get; set; }
        public string large_preview_url { get; set; }
        public string post_processed_id { get; set; }
        public object streaming_url { get; set; }
        public string revert_url { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public string scaled_url { get; set; }
        public string thumbnail_path { get; set; }
        public string preview_path { get; set; }
        public string large_preview_path { get; set; }
        public string status { get; set; }
    }
    public class Stats
    {
        public int following { get; set; }
        public int followers { get; set; }
        public int updates { get; set; }
        public object first_reply_id { get; set; }
        public object first_reply_at { get; set; }
        public int latest_reply_id { get; set; }
        public string latest_reply_at { get; set; }
        public int shares { get; set; }
    }