0

我正在尝试通过 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 发布我的消息。喜欢这个链接

但不幸的是,我收到“内部服务器错误”[500]。喜欢这个问题

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

以及,如何获取pending_attachment 列表?

4

2 回答 2

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. 多部分/表单数据,边界=AaB03x -> 多部分/表单数据;边界=AaB03x
  2. 第一行没有空行
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--
于 2013-08-02T10:05:22.747 回答
0

我坚持了几天只是为了发布带有附件的消息。我不知道如何使用附件 N 发布消息,但它正在使用 pending_attachmentN 方法。

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

我将示例称为我的基线代码。然后我与这个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 类及其属性类

    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; }
    }
于 2017-08-22T08:41:30.653 回答