From 7117b2df3e0d72627d8b9bf5efab8e7982eb4f37 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Thu, 5 Aug 2010 22:49:27 +0100 Subject: [PATCH] Added new post method and get method. Deprecated older getProtectedResource method --- lib/oauth.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 64af64e..0678963 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -141,7 +141,7 @@ exports.OAuth.prototype._createClient= function( port, hostname, sshEnabled, cre return http.createClient(port, hostname, sshEnabled, credentials); } -exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, callback ) { +exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, callback ) { var oauthParameters= { "oauth_timestamp": this._getTimestamp(), "oauth_nonce": this._getNonce(this._nonceSize), @@ -207,7 +207,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke headers["Accept"]= "*/*" headers["Connection"]= "close" headers["User-Agent"]= "Express authentication" - headers["Content-length"]= 0 + headers["Content-length"]= post_body ? post_body.length : 0; //Probably going to fail if not posting ascii headers["Content-Type"]= "application/x-www-form-urlencoded" var path; @@ -233,7 +233,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke }); request.socket.addListener("error",callback); - + if( method == "POST" && post_body != null && post_body != "" ) { + request.write(post_body); + } request.end(); } @@ -245,7 +247,7 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s extraParams.oauth_verifier= oauth_verifier; } - this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, function(error, data, response) { + this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", function(error, data, response) { if( error ) callback(error); else { var results= querystring.parse( data ); @@ -257,8 +259,18 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s } }) } + +// Deprecated exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) { - this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, callback ); + this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", callback ); +} + +exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) { + this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", callback ); +} + +exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, callback) { + this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, null, post_body, callback ); } exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) { @@ -271,7 +283,7 @@ exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) { if( this._authorize_callback ) { extraParams["oauth_callback"]= this._authorize_callback; } - this._performSecureRequest( null, null, "POST", this._requestUrl, extraParams, function(error, data, response) { + this._performSecureRequest( null, null, "POST", this._requestUrl, extraParams, "", function(error, data, response) { if( error ) callback(error); else { var results= querystring.parse(data);