'use strict';
var helper = require('./helper');
Abstraction for the authentication methods of Twitter’s API.
Notice: At the moment this is depending on request
‘s ability to create OAuth
signatures, but in the future this should have it’s own OAuth signing with
OAuth2 support.
var TwitterLib = require('twitter-rest-lite'),
oauth = new TwitterLib.OAuth(var_with_keys);
api.requestToken(callback);
api.accessToken(token, verifier, callback);
api.authenticate(callback);
api.authorize(callback);
uri
- Object with the basic API URI’sopts
- Object with the following paramsconsumer_key
- [Required] consumer_key from Twitterconsumer_secret
- [Required] consumer_secret from Twittercallback
- [Optional]An Object
with methods requestToken
, accessToken
, authenticate
and authorize
.
function OAuth(uri, opts) {
this.uri = uri;
/* Extending `uri` with oauth URI's */
this.uri.requestToken = 'https://api.twitter.com/oauth/request_token';
this.uri.accessToken = 'https://api.twitter.com/oauth/access_token';
this.uri.authenticate = 'https://api.twitter.com/oauth/authenticate';
this.uri.authorize = 'https://api.twitter.com/oauth/authorize';
/* checking the required arguments */
['consumer_key', 'consumer_secret'].forEach(function (item) {
if (opts[item] == null)
throw new Error('There\'s a required argument missing: ' + item);
});
this.opts = opts;
}
callback
- Callback
FunctionReturns a callback with an Error Object
as first parameter if there was
(otherwise just null
) and an Object
with the response with the model:
{
token: String,
token_secret: String,
oauth_callback_confirmed: Boolean
}
oauth.requestToken(function (err, response) {
if (err)
throw err;
console.log(response);
});
response.token
is used by oauth.authenticate
and
oauth.authorize
.
OAuth.prototype.requestToken = function(callback) {
var self = this;
var request = require('request');
var oauth = {};
['consumer_key', 'consumer_secret', 'callback'].forEach(function (e) {
if (self.opts[e] != null)
oauth[e] = self.opts[e];
});
request({
method: 'POST',
uri: self.uri.requestToken,
oauth: oauth
}, function (err, response, body) {
if (err)
return callback(err);
if (response.statusCode !== 200) {
return callback(new Error(
'Twitter:OAuth.requestToken received an status differente than 200: \n' +
'Status Code: ' + response.statusCode + '\n' +
'Body: \n' + body
));
}
var qs = require('querystring');
return callback(null, qs.parse(body));
});
};
token
- String
with oauth_token
verifier
- String
with oauth_verifier
callback
- Callback
FunctionA Callback
with an Error
object as first parameter if there was
(otherwise just null
) and an Object
with the response with the model:
{
oauth_token: String,
oauth_token_secret: String,
user_id: String,
screen_name: String
}
After running either oauth.authenticate
or oauth.authorize
and
making the proper request to twitter’s servers you will end up with
a token
and a verifier
. Suppose they are stored each in a variable
of the same name, then:
oauth.accessToken(token, verifier, function (err, response) {
if (err)
throw (err);
console.log(response);
});
With the data from that response you can initialize the API module and
start GET
‘ing and POST
‘ing with user context as Twitter calls it.
OAuth.prototype.accessToken = function(token, verifier, callback) {
var self = this;
var request = require('request');
[token, verifier].forEach(function (item) {
if (item == null) {
return callback(new Error(
'Twitter:OAuth.accessToken requires all the arguments to work.'
));
}
});
var oauth = {
consumer_key: self.opts.consumer_key,
consumer_secret: self.opts.consumer_secret,
token: token,
verifier: verifier
};
request({
method: 'POST',
uri: self.uri.accessToken,
oauth: oauth
}, function(err, response, body) {
if (err)
return callback(err);
var qs = require('querystring');
return callback(null, qs.parse(body));
});
};
token
- [Required] String
with oauth_token
from
OAuth.requestToken
.cb
- Callback
FunctionA Callback
with an Error
object as the first parameter and a String
with the URL to which redirect users as second parameter.
oauth.authenticate(token, function (err, response) {
if (err)
throw err;
console.log(response);
/* https://api.twitter.com/oauth/authenticate?oauth_token= + token provided */
});
OAuth.prototype.authenticate = function(token, cb) {
helper.authyThing.call(this, 'authenticate', token, cb);
};
token
- [Required] String
with oauth_token
from
OAuth.requestToken
.cb
- Callback
FunctionA Callback
with an Error
object as the first parameter and a String
with the URL to which redirect users as second parameter.
oauth.authorize(token, function (err, response) {
if (err)
throw err;
console.log(response);
/* https://api.twitter.com/oauth/authorize?oauth_token= + token provided */
});
OAuth.prototype.authorize = function(token, cb) {
helper.authyThing.call(this, 'authorize', token, cb);
};
module.exports = OAuth;