0

I'm trying to upload files to yammer via REST Api.

the API say ,i could use

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).

then i try to post my message via WebRequest.like this link

but unfortunately, i got "Internal Server Error"[500]. like this question

is anyone could tell me ,how to upload a file to yammer?

and , how to get a pending_attachment list?

Community
  • 1
  • 1
GeminiYellow
  • 1,016
  • 2
  • 12
  • 34

2 Answers2

0

Well, finally , I know the reason for the failure . request format.

RFC 1867say:

    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--

there two points

  1. multipart/form-data, boundary=AaB03x -> multipart/form-data; boundary=AaB03x
  2. no empty line at first line
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--
Community
  • 1
  • 1
GeminiYellow
  • 1,016
  • 2
  • 12
  • 34
  • Hi @geminiyellow, I'm trying to upload a file to yammer as well and receiving a whole spectrum of errors. I remember one of these approaches also made me face 500 server error so if you can explain what were your finding in a little more simpler terms, it will be a great help. Thank you. – S4nd33p Dec 04 '16 at 16:00
  • @S4nd33p sorry, that question was posted 3 years ago, i dont use yammer now. hope anybody can help you . – GeminiYellow Dec 07 '16 at 08:27
  • At least you responded. Thank you, i'll keep looking. :) – S4nd33p Dec 07 '16 at 09:51
0

I stuck for days just to post message with attachment. I have no idea how to post message with attachmentN, but it is working with pending_attachmentN method.

First you have to call pending_attachment API and get the result ID. Then assign the ID into pending_attachmentN on message API.

I refer to sample as my baseline code. Then I merged with this C# HttpClient 4.5 multipart/form-data upload

Find below code to post message with attachment to Yammer. Hope can save your days.

    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 and its property 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; }
    }
Eric
  • 74
  • 4
  • How did you decipher the return type of the PendingAttachment API call? Can you please share the format? – Saurav Sircar Dec 01 '17 at 07:05
  • @SauravSircar you can read it from the return of ReadAsStringAsync() method. It will be a json string. Then, you need to decode it, as it contains some escape character. Last, on Visual Studio, open Edit Menu -> Paste Special -> Paste JSON as Classes. Voila! you just created the class of the Json string and can be used to deserialize into object. anyway I will update my post to include the PendingAttachment class – Eric Dec 06 '17 at 01:35
  • Thank you @Eric! I also wanted to ask, what does the line `pa = Newtonsoft.Json.JsonConvert.DeserializeObject(result);` do, and how can I include it into my project? I need the output to be proper, so I need that line to work properly. I'm just using Visual Studio to make an extension for another application. – Saurav Sircar Dec 06 '17 at 10:34
  • its doing deserialize object from string into an object. Im using Newtonshoft 3rd party library, you can add this into your project via Manage Nuget Package. – Eric Dec 08 '17 at 01:17