'use strict';
A lightweight Twitter REST-API library with OAuth
and basic POST
/GET
API requests modules.
For more convenient methods you should check twitter-rest
.
var API = require('./lib/api');
var OAuth = require('./lib/oauth');
var TwitterLib = require('twitter-rest-list'),
twitter = new TwitterLib({
consumer_key: "blahblahblah",
consumer_secret: "blahblahblah",
token: 'blah',
token_secret: 'blah',
callback: "randomurl"
});
twitter.api.get('/statuses/user_timeline.json', {
screen_name: 'twitter',
count: 1
}, function (err, response) {
if (err) throw err;
console.log(response);
});
/ #### PLEASE BE WARNED
Using the complete require
is only recommended if token
and
token_secret
already exists.
Otherwise the API module will throw an Error since it does need those two variables to do any of the calls./
Initializes two objects: api
and oauth
. You can initialize them
separated too (this is my preferred method).
All the exported functions expect an Object with the params:
consumer_key
- (Required) consumer key given by Twitterconsumer_secret
- (Required) consumer secret given by Twittertoken
- (Optional) access_token key given by Twittertoken_secret
- (Required if access_token_key
was given)
given by Twittercallback
- (Optional) If your app is a desktop app write oob
(Out-Of-Band); if not then you should write your callback URL here
(which will rewrite the one configured on Twitter’s developer dashboard.Base URIs for Twitter API (These should be overwritten if to be used with a compatible API)
var uri = {
base: 'https://api.twitter.com/1.1',
search: 'https://api.twitter.com/1.1/search'
};
var TwitterLib = require('twitter-rest-lite'),
keys = {consumer_key: '...', consumer_secret: '...', token: '...', token_secret: '...' callback: '...'},
twitter = new TwitterLib(keys);
/* twitter.oauth object */
twitter.oauth.requestToken( /* ... */ );
twitter.oauth.accessToken( /* ... */ );
twitter.oauth.authenticate( /* ... */ );
twitter.oauth.authorize( /* ... */ );
/* twitter.api object */
twitter.api.get( /* ... */ );
twitter.api.post( /* ... */ );
Using the complete require
is only recommended if token
and
token_secret
already exists.
Otherwise the API module will throw an Error since it does need those two variables to do any of the calls.
module.exports = function(opts) {
return {
API: new API(uri, opts),
OAuth: new OAuth(uri, opts)
};
};
var TwitterLib = require('twitter-rest-lite'),
toauth = new TwitterLib.OAuth({
consumer_key: 'blah',
consumer_secret: 'blah',
callback: 'randomurl'
});
toauth.requestToken(function (err, response) {
if (err)
throw err;
console.log(response);
});
More on the OAuth module documentation.
module.exports.OAuth = module.exports.oauth = function(opts) {
return new OAuth(uri, opts);
};
var TwitterLib = require('twitter-rest-lite'),
tapi = new TwitterLib.API({
consumer_key: 'blah',
consumer_secret: 'blah',
token: 'blah',
token_secret: 'blah',
callback: 'randomurl'
});
tapi.get('/statuses/user_timeline.json', {
screen_name: 'twitter'
}, function (err, response) {
if (err)
throw err;
console.log(response);
});
More on the API module documentation.
module.exports.API = module.exports.api = function(opts) {
return new API(uri, opts);
};
module.exports.helper = require('./lib/helper');