Last active
August 29, 2015 14:20
-
-
Save jasonhargrove/4eef30f3fcf3457db5ac to your computer and use it in GitHub Desktop.
Twitter Media Upload using Node OAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// there are proprietary objects here not included in this unpolished snippet | |
// but you should get the gist | |
var OAuth = require('oauth'); | |
var oauth = new OAuth.OAuth( | |
'https://api.twitter.com/oauth/request_token', | |
'https://api.twitter.com/oauth/access_token', | |
consumer_key, | |
consumer_secret, | |
'1.0A', | |
'http://yourdomain.com:8080/twitter/oauth', | |
'HMAC-SHA1' | |
); | |
// in my use case I've confirmed user has the required credentials | |
// otherwise they are generated with browser Twitter log in | |
userService.getUser('KrBbjuDXej', function (user) { | |
var key = data.key + '.jpg'; | |
var keySplit = key.split('/'); | |
var filename = keySplit[keySplit.length - 1]; | |
var tokens = user.get('tokens'); | |
return s3.getObject({ | |
Bucket: 'fashionweek', | |
Key: key | |
}, | |
function (err, data) { | |
oauth.post('https://upload.twitter.com/1.1/media/upload.json', | |
tokens.twitter.oauth_access_token, | |
tokens.twitter.oauth_access_token_secret, | |
{ media_data: data.toString('base64') }, | |
'', | |
function (err, data, res) { | |
if (err) { | |
throw err; | |
} | |
data = JSON.parse(data); | |
cb(data.media_id_string); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above leaves hints for handling authentication. In my case I'm using 3-legged authorization after
oauth
object is created in order to generate the access token and secret.In the callback
cb
, you would then attach the media id(s) to your status update request > https://dev.twitter.com/rest/reference/post/statuses/updateDocs for media/upload > https://dev.twitter.com/rest/reference/post/media/upload
Note that you should use
media_id_string
value instead of the numeric option, which breaks in JavaScript. https://twittercommunity.com/t/media-upload-returning-different-media-id-and-media-id-string-today/37158