简体   繁体   中英

error 403 on nodejs when running .js file

help pls, I am trying to do a POST on my api but I am getting error 403, I read many topics but still not resolved my problem. I am running my js file on nodejs prompt using comand: node myfilename.js to compile and getting this error. below is my code. I was supposed to get a json file back from the site I am trying to consume.

var app = require('./config/customs-express')();
var unirest = require('unirest');
var crypto = require('crypto');
var qs     = require('querystring');

app.listen(3000, function() {
console.log('Server running door 3000');
});

var MB_TAPI_ID  = 'xxx';
var REQUEST_HOST = 'https://www.xxxx.net';
var REQUEST_PATH = '/tapi/v3';
var MB_TAPI_SECRET = 'xxx';

var tapi_nonce = Math.round(new Date().getTime() / 1000);

var tapi_method = 'list_orders';

var params = (tapi_method, tapi_nonce);

var params_string = ((REQUEST_PATH) + '?' + (params));

var tapi_mac = crypto.createHmac('sha512', MB_TAPI_SECRET)
               .update(tapi_method + ':' + MB_TAPI_SECRET + ':' +         
tapi_nonce)
               .digest('hex');

                unirest.post(REQUEST_HOST)
  .headers({'Content-type': 'application/x-www-form-urlencoded'})
  .headers({'Key': MB_TAPI_ID})
  .headers({'Sign': tapi_mac})
  .send(qs.stringify({'method': tapi_method, 'tonce': tapi_nonce}))
  .send(qs.stringify(params_string))
  .end(function (response) {
    console.log(response.body);
});
var app = require('./config/customs-express')();
var unirest = require('unirest');
var crypto = require('crypto');
var qs     = require('querystring');

app.listen(3000, function() {
console.log('Server running door 3000');
});

403 means forbidden. The API is telling you "no."

If this works when you visit the page, it probably means they are using cookies. If that is the case, first hit the login page, get a cookie, then send the login request with the cookie. Superagent can do this , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM