【问题标题】:Uploading a file via Yammer API通过 Yammer API 上传文件
【发布时间】:2014-07-09 21:14:57
【问题描述】:

我可以发布消息,但是当我添加附件或 pending_attachment 时,我收到一条错误消息:

TypeError:在未实现接口 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
            });
        }
    });
}

【问题讨论】:

    标签: javascript api yammer


    【解决方案1】:

    yammer 的 javascript SDK 不适用于附件。 (至少在互联网上没有看到任何工作示例)要上传附件,您可以将文件上传到您的服务器,然后使用 og_url 在您的服务器上发布指向该文件的链接,或者制作您自己的 ajax 表单上传.这是一个例子:

            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。

    【讨论】:

    【解决方案2】:

    它对我有用:

    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>

    【讨论】:

    【解决方案3】:

    //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
    
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2018-07-31
      • 2011-10-18
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多