var querystring = require('querystring')
  , request = require('request')
  , _ = require('underscore')._
  ;

/**
 * @private
 */
function postParams(url, params, callback) {
  request({
    method : 'POST',
    url : url,
    body : querystring.stringify(params),
    headers : {
      "content-type" : "application/x-www-form-urlencoded"
    }
  }, function(err, response) {
    if (err) {
      callback(err);
      return;
    }
    var res;
    try {
      res = JSON.parse(response.body);
      if (response.statusCode >= 400) {
        err = res;
        err.message = err.error + ": " + err.error_description;
        res = undefined;
      }
    } catch(e) {
      err = e;
    }
    callback(err, res);
  });
}

/**
 *
 */
var defaults = {
  loginUrl : "https://login.salesforce.com"
};

/**
 * OAuth2
 */
var OAuth2 = module.exports = function(options) {
  if (options.authzServiceUrl && options.tokenServiceUrl) {
    this.loginUrl = options.authzServiceUrl.split('/').slice(0, 3).join('/');
    this.authzServiceUrl = options.authzServiceUrl;
    this.tokenServiceUrl = options.tokenServiceUrl;
  } else {
    this.loginUrl = options.loginUrl || defaults.loginUrl;
    this.authzServiceUrl = this.loginUrl + "/services/oauth2/authorize";
    this.tokenServiceUrl = this.loginUrl + "/services/oauth2/token";
  }
  this.clientId = options.clientId;
  this.clientSecret = options.clientSecret;
  this.redirectUri = options.redirectUri;
};



/**
 *
 */
_.extend(OAuth2.prototype, {
  
  /**
   *
   */
  getAuthorizationUrl : function(params) {
    params = _.extend({
      response_type : "code",
      client_id : this.clientId,
      redirect_uri : this.redirectUri
    }, params || {});
    return this.authzServiceUrl + 
      (this.authzServiceUrl.indexOf('?') >= 0 ? "&" : "?") + 
      querystring.stringify(params);
  }
  ,

  /**
   * OAuth2 Refresh Token Flow
   */
  refreshToken : function(refreshToken, callback) {
    postParams(this.tokenServiceUrl, {
      grant_type : "refresh_token",
      refresh_token : refreshToken,
      client_id : this.clientId,
      client_secret : this.clientSecret
    }, callback);
  }
  ,

  /**
   * OAuth2 Web Server Authentication Flow (Authorization Code)
   * Access Token Request
   */
  requestToken : function(code, callback) {
    postParams(this.tokenServiceUrl, {
      grant_type : "authorization_code",
      code : code,
      client_id : this.clientId,
      client_secret : this.clientSecret,
      redirect_uri : this.redirectUri
    }, callback);
  }
  ,

  /**
   * OAuth2 Username-Password Flow (Resource Owner Password Credentials)
   */
  authenticate : function(username, password, callback) {
    postParams(this.tokenServiceUrl, {
      grant_type : "password",
      username : username,
      password : password,
      client_id : this.clientId,
      client_secret : this.clientSecret,
      redirect_uri : this.redirectUri
    }, callback);
  }
});




@@ -33,20 +33,21 @@
 string')
+,
 %0A  
-,
+ 
  request
@@ -67,20 +67,43 @@
 equest')
+,
 %0A  
-,
+  Q = require('q'),%0A   
  _ = req
@@ -122,19 +122,16 @@
 core')._
-%0A  
 ;%0A%0A/**%0A 
@@ -196,16 +196,40 @@
 %7B%0A  
+var promise = Q.nfcall(
 request
-(
+, 
 %7B%0A  
@@ -388,18 +388,23 @@
    %7D%0A  %7D
-, 
+).then(
 function
@@ -408,21 +408,16 @@
 ion(
-err, 
 response
 ) %7B%0A
@@ -416,93 +416,59 @@
 onse
-) %7B%0A    if (err) %7B%0A      callback(err);%0A      return;%0A    %7D%0A    var res;%0A    try %7B%0A  
+s) %7B%0A    var response = responses%5B0%5D;%0A    var res;%0A
     
@@ -500,18 +500,16 @@
 .body);%0A
-  
     if (
@@ -548,46 +548,31 @@
     
- 
+var
  err = 
-res;%0A        err.message = err
+new Error(res
 .err
@@ -583,19 +583,19 @@
  %22: %22 + 
-err
+res
 .error_d
@@ -604,24 +604,25 @@
 cription
+)
 ;%0A      
   res = 
@@ -617,76 +617,83 @@
     
-  res = undefined;%0A      %7D%0A    %7D catch(e) %7B%0A      err = e;%0A  
+err.name = res.error;%0A      throw err;%0A    %7D%0A    return res;%0A
   %7D
+);
 %0A  
- 
+return
  cal
@@ -701,24 +701,46 @@
 back
-(err, res);%0A  %7D)
+ ? promise.nodeify(callback) : promise
 ;%0A%7D%0A
@@ -1853,27 +1853,24 @@
 params);%0A  %7D
-%0A  
 ,%0A%0A  /**%0A   
@@ -1951,32 +1951,39 @@
 callback) %7B%0A    
+return 
 postParams(this.
@@ -2159,35 +2159,32 @@
 , callback);%0A  %7D
-%0A  
 ,%0A%0A  /**%0A   * OA
@@ -2312,32 +2312,39 @@
 callback) %7B%0A    
+return 
 postParams(this.
@@ -2555,19 +2555,16 @@
 ck);%0A  %7D
-%0A  
 ,%0A%0A  /**
@@ -2705,16 +2705,23 @@
 ) %7B%0A    
+return 
 postPara

make oauth2 function call promised