通过Yammer API上传文件

时间:2014-07-09 21:14:58

标签: javascript api yammer

我能够发布消息,但是当我添加附件或pending_attachment时,我收到错误消息:

TypeError:' stepUp'调用了一个没有实现接口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
            });
        }
    });
}

3 个答案:

答案 0 :(得分:2)

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。

答案 1 :(得分:2)

它对我有用:

having
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.");
		}
	});
}

答案 2 :(得分:0)

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

});