lib/oauth2_client.js
function(data, code, callback) {
    var self = this;
    var cconfig = this.config.client;
    var sconfig = this.config.servers[data.oauth2_server_id];
    request.post({uri: sconfig.server_token_endpoint,
                  headers: {'content-type': 'application/x-www-form-urlencoded'},
                  body: querystring.stringify({
                    grant_type: "authorization_code",
                    client_id: sconfig.client_id,
                    code: code,
                    client_secret: sconfig.client_secret,
                    redirect_uri: cconfig.redirect_uri
                  })
                 }, function(error, response, body) {
                   console.log(body);
                   if (!error && response.statusCode == 200) {
                     try {
                       var methods = self.methods[data.oauth2_server_id];
                       var token = methods.transform_token_response(body)
                       callback(null, token);
                     } catch(err) {
                       callback(err);
                     }
                   } else {
                     // TODO: check if error code indicates problem on the client,
                     console.error(error, body);
                     callback(error);
                   }
                 });
  }
function(data, code, callback) {
    var self = this;
    var cconfig = this.config.client;
    var sconfig = this.config.servers[data.oauth2_server_id];
    request.post({uri: sconfig.server_token_endpoint,
                  headers: {'content-type': 'application/x-www-form-urlencoded'},
                  body: querystring.stringify({
                    grant_type: "authorization_code",
                    client_id: sconfig.client_id,
                    code: code,
                    client_secret: sconfig.client_secret,
                    redirect_uri: cconfig.redirect_uri
                  })
                 }, function(error, response, body) {
                   console.log(body);
                   if (!error && response.statusCode == 200) {
                     try {
                       var methods = self.methods[data.oauth2_server_id];
                       var token = methods.transform_token_response(body)
                       callback(null, token);
                     } catch(err) {
                       callback(err);
                     }
                   } else {
                     // TODO: check if error code indicates problem on the client,
                     console.error(error, body);
                     callback(error);
                   }
                 });
  }
function ( data , code , callback ) { var self = this ; var cconfig = this . config . client ; var sconfig = this . config . servers [ data . oauth2_server_id ] ; request . post ( { uri : sconfig . server_token_endpoint , headers : { 'content-type' : 'application/x-www-form-urlencoded' } , body : querystring . stringify ( { grant_type : "authorization_code" , client_id : sconfig . client_id , code : code , client_secret : sconfig . client_secret , redirect_uri : cconfig . redirect_uri } ) } , function ( error , response , body ) { console . log ( body ) ; if ( ! error && response . statusCode == 200 ) { try { var methods = self . methods [ data . oauth2_server_id ] ; var token = methods . transform_token_response ( body ) callback ( null , token ) ; } catch ( err ) { callback ( err ) ; } } else { // TODO: check if error code indicates problem on the client, console . error ( error , body ) ; callback ( error ) ; } } ) ; }
Valid the grant given by user requesting the OAuth2 server
at OAuth2 token endpoint.

Arguments:
- data: hash containing:
- oauth2_server_id: string, the OAuth2 server is (ex: "facebook.com").
- next_url: string, the next_url associated with the OAuth2 request.
- state: hash, state associated with the OAuth2 request.
- code: the authorization code given by OAuth2 server to user.
- callback: function to be called once grant is validated/rejected.
Called with the access_token returned by OAuth2 server as first
parameter. If given token might be null, meaning it was rejected
by OAuth2 server.
Valid the grant given by user requesting the OAuth2 server at OAuth2 token endpoint .