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

Commit 7a0144d

Browse files
author
Tim Whitlock
committed
added oauth utils for fetching request and access tokens
1 parent b1ae9b8 commit 7a0144d

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

examples/oauth-complete.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Example of completing OAuth flow by requesting access token from authorized request token
3+
*/
4+
5+
6+
var client = require('../lib/twitter').createClient();
7+
8+
client.setAuth (
9+
'your consumer key',
10+
'your consumer secret',
11+
'request token from step 1',
12+
'request secret from step 1'
13+
);
14+
15+
var verifier = 'PIN or code from user flow';
16+
17+
client.fetchAccessToken( verifier, function( token, raw, status ){
18+
if( token ){
19+
console.log('Credentials for @'+raw.screen_name );
20+
console.log(' > Access key: '+token.key );
21+
console.log(' > Access secret: '+token.secret );
22+
}
23+
else {
24+
console.error('Status '+status+', failed to fetch access token');
25+
}
26+
} );

examples/oauth-start.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Example of starting OAuth flow by fetch a request token.
3+
*/
4+
5+
6+
var client = require('../lib/twitter').createClient();
7+
8+
client.setAuth (
9+
'your consumer key',
10+
'your consumer secret'
11+
);
12+
13+
client.fetchRequestToken( 'oob', function( token, raw, status ){
14+
if( token ){
15+
console.log('Request secret: '+token.secret );
16+
console.log('Authorize at: '+ token.getAuthorizationUrl() );
17+
}
18+
else {
19+
console.error('Status '+status+', failed to fetch request token');
20+
}
21+
} );

lib/twitter.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function OAuthToken( key, secret ){
2929
this.secret = secret;
3030
}
3131

32-
OAuthToken.prototype.get_authorization_url = function(){
32+
OAuthToken.prototype.getAuthorizationUrl = function(){
3333
return TWITTER_OAUTH_AUTHORIZE_URL+'?oauth_token='+encodeURIComponent(this.key);
3434
}
3535

@@ -150,6 +150,9 @@ TwitterClient.prototype.post = function( requestPath, requestArgs, callback ){
150150
}
151151

152152
TwitterClient.prototype._rest = function( requestMethod, requestPath, requestArgs, callback ){
153+
if( ! this.hasAuth() ){
154+
throw new Error('Twitter REST client not authenticated');
155+
}
153156
var requestUri = TWITTER_API_BASE + '/' + requestPath + '.json';
154157
return this.call( requestMethod, requestUri, requestArgs, function( res ) {
155158
// started to receive response from twitter
@@ -183,6 +186,9 @@ TwitterClient.prototype._rest = function( requestMethod, requestPath, requestArg
183186
}
184187

185188
TwitterClient.prototype.stream = function( requestPath, requestArgs, callback ){
189+
if( ! this.hasAuth() ){
190+
throw new Error('Twitter streaming client not authenticated');
191+
}
186192
var requestMethod = 'GET',
187193
requestUri = TWITTER_STREAM_BASE+'/'+requestPath+'.json';
188194
if( 'user' === requestPath ){
@@ -220,15 +226,12 @@ TwitterClient.prototype.stream = function( requestPath, requestArgs, callback ){
220226
}
221227

222228
TwitterClient.prototype.call = function( requestMethod, requestUri, requestArgs, callback ){
223-
if( ! this.hasAuth() ){
224-
throw new Error('Twitter client not authenticated');
225-
}
226229
requestMethod = String( requestMethod||'GET' ).toUpperCase();
227230
// build and sign request parameters
228-
var params = new OAuthParams( requestArgs )
229-
.setConsumer( this.consumerToken )
230-
.setAccess( this.accessToken )
231-
.sign( requestMethod, requestUri );
231+
var params = new OAuthParams( requestArgs );
232+
this.consumerToken && params.setConsumer( this.consumerToken );
233+
this.accessToken && params.setAccess( this.accessToken );
234+
params.sign( requestMethod, requestUri );
232235
// grab authorization header and any remaining params
233236
var oauth = params.getHeader(),
234237
query = params.serialize();
@@ -267,9 +270,33 @@ TwitterClient.prototype.abort = function(){
267270
}
268271
}
269272

273+
TwitterClient.prototype.fetchRequestToken = function( url, callback ){
274+
var requestUri = TWITTER_OAUTH_REQUEST_TOKEN_URL,
275+
requestArgs = { oauth_callback: url||'oob' };
276+
return this._oauthExchange( requestUri, requestArgs, callback );
277+
}
270278

279+
TwitterClient.prototype.fetchAccessToken = function( verifier, callback ){
280+
var requestUri = TWITTER_OAUTH_ACCESS_TOKEN_URL,
281+
requestArgs = { oauth_verifier: verifier };
282+
return this._oauthExchange( requestUri, requestArgs, callback );
283+
}
271284

272-
285+
TwitterClient.prototype._oauthExchange = function( requestUri, requestArgs, callback ){
286+
this.call( 'POST', requestUri, requestArgs, function( res ){
287+
// started to receive response from twitter
288+
res.setEncoding('utf8');
289+
var body = '';
290+
res.on('data', function( chunk ) {
291+
body += chunk;
292+
} );
293+
res.on('end', function(){
294+
var params = require('querystring').parse( body );
295+
var token = params.oauth_token && params.oauth_token_secret && new OAuthToken( params.oauth_token, params.oauth_token_secret );
296+
'function' == typeof callback && callback( token, params, res.statusCode );
297+
} );
298+
} );
299+
}
273300

274301

275302

0 commit comments

Comments
 (0)