Twitter image upload error: media parameter missing

In a new twitter upload file , said

First use the POST files multipart / form-data or base64 in https://upload.twitter.com/1.1/media/upload.json .

but get an error: media code 38 is missing.

here is my code

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

I am reusing code from this topic: Uploading images to twitter API from node.js

Any suggestion? Thanks you

+2
source share
2 answers

Here is what I use to download media:

(I use OAuth to create Oauth authentication)

I also add /statuses/update.json to associate a new message with 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"); } }); } }); 

Enjoy

+6
source

Php solution

Libraries taken from here: https://github.com/abraham/twitteroauth Check image limit here: https://dev.twitter.com/rest/reference/get/help/configuration

The failure is to send base64_encode data and a size that should not exceed the image limit (default is 3 MB), otherwise you will get "not recognized". mistake.

Here is the code (only for downloading images) that worked for me: require_once ('includes /Twitter/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); 
+1
source

All Articles