From 752c4419f91aa038dd53d7f1fba3ee0995954d67 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Fri, 6 Aug 2010 09:01:40 +0100 Subject: [PATCH] Changed the post method to support passing of either a POST body string and optional content-type, or a hash of query parameters that will be url form encoded. --- lib/oauth.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 0678963..49a1fa4 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -48,7 +48,7 @@ exports.OAuth.prototype._decodeData= function(toDecode) { } exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) { - var signatureBase= this._createSignatureBase(method, url, parameters); + var signatureBase= this._createSignatureBase(method, url, parameters); return this._createSignature( signatureBase, tokenSecret ); } @@ -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, post_body, callback ) { +exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) { var oauthParameters= { "oauth_timestamp": this._getTimestamp(), "oauth_nonce": this._getNonce(this._nonceSize), @@ -158,6 +158,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke oauthParameters[key]= extra_params[key]; } } + if( !post_content_type ) { + post_content_type= "application/x-www-form-urlencoded"; + } var parsedUrl= URL.parse( url, false ); if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80; @@ -208,7 +211,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke headers["Connection"]= "close" headers["User-Agent"]= "Express authentication" 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" + headers["Content-Type"]= post_content_type; var path; if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/"; @@ -247,7 +250,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, "", null, function(error, data, response) { if( error ) callback(error); else { var results= querystring.parse( data ); @@ -262,15 +265,25 @@ 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, "", 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 ); + this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", 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.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) { + var extra_params= null; + if( typeof post_content_type == "function" ) { + callback= post_content_type; + post_content_type= null; + } + if( typeof post_body != "string" ) { + post_content_type= "application/x-www-form-urlencoded" + extra_params= post_body; + post_body= querystring.stringify(post_body); + } + this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback ); } exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) { @@ -283,7 +296,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, "", null, function(error, data, response) { if( error ) callback(error); else { var results= querystring.parse(data);