简体   繁体   English

通过 Yammer API 上传文件

[英]Uploading a file via Yammer API

I'm able to post a message but when I add either the attachment or pending_attachment, I get an error saying:我可以发布消息,但是当我添加附件或待定附件时,我收到一条错误消息:

TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement.类型错误:在未实现接口 HTMLInputElement 的对象上调用了“stepUp”。

function post() {
    yam.getLoginStatus( function(response) {
        if (response.authResponse) {

            yam.request(
              { url: "https://api.yammer.com/api/v1/messages.json" //note:  the endpoint is api.yammer...
              , method: "POST"
              , data: {
                "body" : document.getElementById("post_body").value,
                "group_id" : document.getElementById("group_id").value
                ,"attachment1" : document.getElementById("attachment")
              }
              , success: function (msg) {
                    alert("Post was Successful!: " + msg.messages[0].id); //id of new message
              }
              , error: function (msg) { alert("Post was Unsuccessful..." + msg); }
              }
            );
        } else {
            yam.login( function (response) {
               //nothing
            });
        }
    });
}

yammer's javascript SDK doesn't work with attachment. yammer 的 javascript SDK 不适用于附件。 (at least no working example has been seen on the internet) To upload an attachment, you can either upload the file to your server and then use og_url to post a link to that file on your server, or cook up your own ajax form upload. (至少在互联网上没有看到工作示例)要上传附件,您可以将文件上传到您的服务器,然后使用 og_url 在您的服务器上发布该文件的链接,或者编写您自己的 ajax 表单上传. here is an example:这是一个例子:

        var data = new FormData();

        data.append('body', document.getElementById("post_body").value);
        data.append('group_id', document.getElementById("group_id").value);


        data.append('attachment1', document.getElementById("attachment"), 'filename_of_your_choice');


        $.ajax({
            url: "https://api.yammer.com/api/v1/messages.json",
            data: data,
            beforeSend: function (xhr) {
                // set authorization header
                xhr.setRequestHeader("Authorization", "Bearer YOUR_AUTHORIZATION_TOKEN");
            },
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function (data) {
                console.log("ajax post success.");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("There was an error with the request.");
            }
        });

Notice that the authorization token is obtained in the response to a successful login.请注意,授权令牌是在成功登录的响应中获得的。 It is not your app ID.它不是您的应用程序 ID。 Also, I doubt document.getElementById("attachment") will work.另外,我怀疑 document.getElementById("attachment") 会起作用。 You need to convert that object into an byte array blob.您需要将该对象转换为字节数组 blob。

It works for me:这个对我有用:

 function postAttach() { var msg = $('#attach_body').val(); var m_data = new FormData(); m_data.append('body', msg); m_data.append('group_id', 6194208); m_data.append('attachment1', $('input[name=attachment1]')[0].files[0]); yam.platform.request({ url: "messages.json", contentType: "multipart/form-data", data: m_data, processData: false, contentType: false, type: 'POST', dataType: 'json', success: function (user) { alert("The request was successful."); }, error: function (user) {console.log(user); alert("There was an error with the request."); } }); }
 <div name="postYammer"> <input type="text" name="body" value="" id="attach_body" /> <input type="file" name="attachment1" id="attach_img"/> <button onclick="postAttach()" type="button">Post</button> </div>

 //Example Nodejs in async function // nota : you can get token from https://developer.yammer.com/docs/app-registration const qs = require("qs"); const got = require("got"); const formData = require("form-data"); const fs = require("fs"); var json = { is_rich_text: true, topic1: 'tag1', topic2: 'tag2', body: 'body body', group_id: 'group_id', } let querystring = qs.stringify(json); var formData = new formData(); var aHeader = formData.getHeaders(); aHeader['Authorization'] = "Bearer token"; formData.append('attachment1', fs.createReadStream(path)); const response = await got.post('https://www.yammer.com/api/v1/messages.json?' + qs, { headers: aHeader, body: formData });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM