本文介绍了Twitter 图片上传错误:缺少媒体参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在新推特媒体上传api中说
首先,使用 POST multipart/form-data 或 base64 编码文件到 https://upload.twitter.com/1.1/media/upload.json.
但得到错误:代码 38缺少媒体参数."
but get error : code 38 "media parameter is missing."
这是我的代码
var Twitter = require('./twitter.js');
var request = require('request');
var fs = require('fs');
var path = require('path');
var FormData = require('form-data');
var utf8 = require('utf8');
var base64_encode = require('base64').encode;
var oauth = {
consumer_key: '',
consumer_secret: '',
token: '',
token_secret: ''
};
var form = new FormData();
fs.readFile(__dirname + '/image.jpg', function(err, data) {
if (err) {
throw err;
}
var encoded = base64_encode(data);
form.append('media[]', encoded); // I've also tried media instead of media[]
form.getLength(function(err, length) {
var r = request.post({
url: 'https://upload.twitter.com/1.1/media/upload.json',
oauth: oauth,
}, function(error, response, body) {
if (error) {
console.error(error);
return;
}
console.dir(body);
});
r._form = form;
r.setHeader('content-length', length);
});
});
我重用了这个主题的代码:从node.js
I reuse code from this topic : Upload images to twitter API from node.js
有什么建议吗?谢谢
推荐答案
这是我用来上传媒体的内容:
(我只使用 OAuth 来创建 Oauth 认证)
我还添加了/statuses/update.json 以将新帖子链接到您的媒体.
Here is what I use to upload media :
(I only use OAuth to create Oauth authentification)
I also add /statuses/update.json to link a new post to your media.
var oauth = require('oauth');
var fs = require('fs');
var a = new oauth.OAuth("https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
config.consumerKey,
config.consumerSecret,
"1.0",
config.callbackUrl,
"HMAC-SHA1");
a.post("https://upload.twitter.com/1.1/media/upload.json", oauth_access_token, oauth_access_token_secret, {media:fs.readFileSync(file_path).toString("base64")} ,"" , function (e, data, res){
if (e) {
console.error(e);
}else {
try{
data = JSON.parse(data);
}catch (e){
console.error("Error Json : " + e);
}
console.log(data.media_id);
a.post("https://api.twitter.com/1.1/statuses/update.json", oauth_access_token, oauth_access_token_secret, {status:message,media_ids:[data.media_id_string]}, "", function (e, data, res){
if (e) {
console.error(e);
}else {
console.log("Success");
}
});
}
});
玩得开心
这篇关于Twitter 图片上传错误:缺少媒体参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!