Twitter图片上传错误:缺少媒体参数

时间:2014-10-13 13:05:47

标签: javascript node.js twitter

new twitter media upload api

  

首先,将POST multipart / form-data或base64编码文件用于https://upload.twitter.com/1.1/media/upload.json

但得到错误:代码38"缺少媒体参数。"

这是我的代码

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

我重复使用此主题中的代码:Upload images to twitter API from node.js

有什么建议吗? 谢谢

2 个答案:

答案 0 :(得分:6)

以下是我用来上传媒体的内容:
(我只使用OAuth创建Oauth身份验证)
我还添加/statuses/update.json将新帖子链接到您的媒体。

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

玩得开心

答案 1 :(得分:1)

PHP解决方案

从这里取得的图书馆:https://github.com/abraham/twitteroauth 在此处查看您的图片限制:https://dev.twitter.com/rest/reference/get/help/configuration

故障是发送base64_encode数据,大小不超过图像限制(默认为3MB),否则您将收到“无法识别”。错误。

以下是适用于我的代码(仅图像上传内容):     require_once( '包括/叽叽喳喳/ twitteroauth.php');

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET);

$filePath = '/absolute path to the file/';
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$method = 'POST';
$parameters = array(
    'media' => base64_encode(file_get_contents($filePath)),
);
$request = OAuthRequest::from_consumer_and_token($connection->consumer, $connection->token, $method, $url, $parameters);
$request->sign_request($connection->sha1_method, $connection->consumer, $connection->token);

$response = $connection->http($request->get_normalized_http_url(), $method, $request->to_postdata());
if ($connection->format === 'json' && $connection->decode_json) {
    $response = json_decode($response);
}
print_r($response);