Skip to content
This repository has been archived by the owner on Feb 13, 2018. It is now read-only.

Commit

Permalink
Replace deprecated updateWithMedia with uploadMedia
Browse files Browse the repository at this point in the history
Twitter has deprecated the update_with_media endpoint. Use uploadMedia instead.
Closes #23.
  • Loading branch information
reneraab committed Apr 23, 2015
1 parent 4cf1ce1 commit d61a35a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
21 changes: 6 additions & 15 deletions README.md
Expand Up @@ -99,18 +99,9 @@ For more information on the different types of timelines see https://dev.twitter

For Streams you must use _getStream_ which has two instead of just one callback: a dataCallback and an endCallback. (c.f. data and end events of node's http response)

## Use of update_with_media ##
(works similar for update_profile_image)
To send media alongside a tweet you just call the method as specified before. Please note, that you have to specify the parameters slightly different than proposed by the Twitter API documentation:
```javascript
{
media: [
"path_to_file1",
"path_to_file2",
stream
],
status: "Hello World"
},
```
Instead of specifing "media[]", you use a real array. The given paths will then be read and posted to the Twitter API. You can also use a Readable Stream (http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options) instead of a Path.
Please note that Twitter only allows one image at the moment (the last one specified will be used).
## How to upload media ##
To upload media to Twitter, call `twitter.uploadMedia(params, accessToken, accessTokenSecret, callback)` with params containing the following:
* _media_: Either the raw binary content of the image, the binary base64 encoded (see isBase64 below) or the path to the file containing the image.
* _isBase64_: Set to true, if media contains base64 encoded data

For a example result see https://dev.twitter.com/rest/reference/post/media/upload. You can pass multiple media_ids to the statuses/update endpoint by seperating them with commas (e.g. "[id1],[id2],[id3],[id4]").
29 changes: 10 additions & 19 deletions twitter.js
Expand Up @@ -6,6 +6,7 @@ var VERSION = "1.5.0",
fs = require("fs");

var baseUrl = "https://api.twitter.com/1.1/";
var uploadBaseUrl = "https://upload.twitter.com/1.1/";
var authUrl = "https://twitter.com/oauth/authenticate?oauth_token=";

var Twitter = function(options) {
Expand Down Expand Up @@ -246,9 +247,9 @@ Twitter.prototype.statuses = function(type, params, accessToken, accessTokenSecr
}
};

Twitter.prototype.updateWithMedia = function(params, accessToken, accessTokenSecret, callback) {
Twitter.prototype.uploadMedia = function(params, accessToken, accessTokenSecret, callback) {
var r = request.post({
url: baseUrl + "statuses/update_with_media.json",
url: uploadBaseUrl + "media/upload.json",
oauth: {
consumer_key: this.consumerKey,
consumer_secret: this.consumerSecret,
Expand All @@ -257,7 +258,7 @@ Twitter.prototype.updateWithMedia = function(params, accessToken, accessTokenSec
}
}, function(error, response, body) {
if (error) {
callback(error, body, response, baseUrl + "statuses/update_with_media.json?" + querystring.stringify(params));
callback(error, body, response, uploadBaseUrl + "media/upload.json?" + querystring.stringify(params));
} else {
try {
callback(null, JSON.parse(body), response);
Expand All @@ -267,27 +268,17 @@ Twitter.prototype.updateWithMedia = function(params, accessToken, accessTokenSec
}
});

var parameter = (params.isBase64) ? "media_data" : "media";

// multipart/form-data
var form = r.form();
for (var key in params) {
if (key != "media") {
form.append(key, params[key]);
}
}

// append the media array
var media = params["media"];
for (var i = 0; i < media.length; i++) {
// if the content of media[i] is an existing path, create a ReadStream, otherwise just append the content
if (fs.existsSync(media[i])) {
form.append("media[]", fs.createReadStream(media[i]));
} else {
form.append("media[]", media[i]);
}
if (fs.existsSync(params.media)) {
form.append(parameter, fs.createReadStream(params.media));
} else {
form.append(parameter, params.media);
}
};


// Search
Twitter.prototype.search = function(params, accessToken, accessTokenSecret, callback) {
this.oa.get(baseUrl + "search/tweets.json?" + querystring.stringify(params), accessToken, accessTokenSecret, function(error, data, response) {
Expand Down

0 comments on commit d61a35a

Please sign in to comment.