通过 Yammer API 上传文件 [英] Uploading a file via Yammer API

查看:45
本文介绍了通过 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' 在未实现接口 HTMLInputElement 的对象上调用.

TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement.

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 的 javascript SDK 不适用于附件.(至少在互联网上没有看到工作示例)要上传附件,您可以将文件上传到您的服务器,然后使用 og_url 在您的服务器上发布该文件的链接,或者编写您自己的 ajax 表单上传.这是一个例子:

yammer's javascript SDK doesn't work with attachment. (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. 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.");
            }
        });

请注意,授权令牌是在成功登录的响应中获得的.它不是您的应用程序 ID.另外,我怀疑 document.getElementById("attachment") 会起作用.您需要将该对象转换为字节数组 blob.

Notice that the authorization token is obtained in the response to a successful login. It is not your app ID. Also, I doubt document.getElementById("attachment") will work. You need to convert that object into an byte array blob.

这篇关于通过 Yammer API 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆