通过Yammer API上传文件

Uploading a file via Yammer API

本文关键字:文件 API Yammer 通过      更新时间:2023-09-26

我能够发布消息,但当我添加附件或pending_attachment时,我得到一个错误说:

TypeError: ' stepp '在一个没有实现接口的对象上被调用。

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表单上传。下面是一个例子:

        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("附件")是否有效。您需要将该对象转换为字节数组blob。

它适合我:

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