@@ -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
152152TwitterClient . 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
185188TwitterClient . 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
222228TwitterClient . 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