Tchak Tchak - 14 days ago 6
Javascript Question

Nodejs - Posting data with Oauth (here in Open Bank Project)



I try to post data on an open bank project and I can't see what I'm missing in my code. I use the NodeJs sdk, the API explorer and the OAuth.post() example (webapplog.com/node-js-oauth1-0-and-oauth2-0-twitter-api-v1-1-examples).

GET is working fine, however I keep having an error with POST. Here is the code (looks very much like the sdk, with a valid key ans secret, I just write the 2 new app.get).


Note : in the apisandbox openbankproject/index, I created 2 bank accounts, one with id 142842 and 1000EUR and one with id 163248 and 2000EUR.

This one (GET) to get account information works :

app.get('/test', function(req, res){
consumer.get("https://apisandbox.openbankproject.com/obp/v1.2.1/banks/rbs/accounts/142842/owner/account",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function (error, data, response) {
if (error) {console.log('error is ' + error);}
var parsedData = JSON.parse(data);
res.status(200).send(parsedData)
});
});


This one (POST) for payment doesn't :

app.get('/pay', function(req, res){

var requestData = {
account_id : "163248",
bank_id: "rbs" ,
amount: "50.00"
};
consumer.post("https://apisandbox.openbankproject.com/obp/v1.2.1/banks/rbs/accounts/142842/owner/transactions",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
requestData,
"application/json",
function (error, data, response) {
console.log(error);
//console.log(data);
//console.log(response);
});


I get this error :

{ statusCode: 404,
data: '<!DOCTYPE html>\n<html> <body>The Requested URL /obp/v1.2.1/banks/rbs/accounts/142842/owner/transactions was not found on this server</body> </html> \n ' }


Thanks for your help !

Answer

I finally can answer my own question thanks to this link (see from line 165).

app.get('/pay', function(req, res){
  var fromBankId = "rbs";
  var fromAccountId = "163248";

  var toBankId = "rbs";
  var toAccountId = "163248";

  var currency = "EUR";
  var amount = "100.50";

  var description = "description test";


  var transactionRequestType = "SANDBOX_TAN";

  var toObj = {"bank_id": toBankId, "account_id": toAccountId};
  var valueObj = {"currency":currency, "amount":amount};

  var detailsObj = {"to": toObj, "value": valueObj, "description": description}
  var viewId = "owner"

  var details = JSON.stringify(detailsObj);

  var postUrl = "https://apisandbox.openbankproject.com" + "/obp/v2.1.0/banks/" + fromBankId + "/accounts/" + fromAccountId + "/" + viewId + "/transaction-request-types/" + transactionRequestType + "/transaction-requests";

consumer.post(postUrl,
  req.session.oauthAccessToken,
  req.session.oauthAccessTokenSecret,
  details, // This is the body of the request
  "application/json", // Must specify this else will get 404
  function (error, data, response) {

      var error = JSON.stringify(error)

      console.log("error is: " + error)
      console.log("data is: " + data)
      console.log("response is: " + response)


        try {
          var parsedData = JSON.parse(data);
          console.log("parsedData is: " + parsedData)
          message = ""
        } catch (err) {
            // handle the error safely
            console.log(err)
            message = "Something went wrong creating a transaction request - did you supply the correct values?"
        }
    });
});