通过Yammer API上传文件
Uploading a file via Yammer API
我能够发布消息,但当我添加附件或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
});
相关文章:
- 找不到Intel XDK cordova文件api 1.3.3文件
- 如何在IE8中启用JavaScript文件API
- 文件API中的Javascript变量范围
- 我可以使用 HTML5 文件 API 预览 PDF 吗?
- 在NW.js应用程序中为HTML5文件API设置配额
- html5文件api,将用户选择的目录存储在sessionStorage中
- HTML5文件API使用Ajax上传多个图像
- Javascript;HTML文件API-基于文件对象的javascript对象是否仍然包含文件'的位置
- 文件选择/文件API-将文件发送到嵌入式PDFObject
- 使用 HTML 文件 API 和 JavaScript 搜索本地目录
- 如何使用 phonegap 文件 API 在该目录中创建目录和文件
- 使用 Phonegap 文件 API 获取所有文件名
- Cordova 的文件 API 中的元数据
- 如何从特权代码使用 Dom 文件 API
- 有没有办法将文件附加到已经存在的文件列表中?JavaScript 文件 API
- JavaScript 文件 API 具有多个输入
- 兴访问用户配置文件 API
- 使用 HTML5 文件 API 在浏览器中创建音频列表
- 如何从html 5文件API获取图像宽度
- 如何通过文件 API 从 json 文件执行数据