Netsuite OAuth Not Working

EDIT: Just published an npm module which should make things easier: https://www.npmjs.com/package/nsrestlet


Was able to get some code working after hunting through GitHub Code commits. Still, bknights response is really good.

Here’s what I got working.

Assuming you have Node.js and npm installed, run:

npm install request
npm install [email protected]

It’s really important that it’s version 1.0.1.

Once you have that, this code should work:

/*
    =================    REQUIRED USER ACCOUNT INFORMATION    ==============================================
*/

var accountID = 'PUT ACCOUNT ID HERE';

var token = {
    public: 'PUT TOKEN KEY HERE',
    secret: 'PUB TOKEN SECRET HERE'
};

var consumer = {
    public: 'PUT CONSUMER KEY HERE',
    secret: 'PUT CONSUMER SECRET HERE'
};

//use the full restlet URL, not the rest.netsuite.com URL
//for example, https://YOURACCOUNTNUMBER.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=SCRIPTNUMBER&deploy=DEPLOYNUMBER
var restlet_url="PUT YOUR RESTLET URL HERE";

/*
    =========================================================================================================
*/

//REQUIRED NPM MODULES
const request = require('request');
const OAuth   = require('oauth-1.0a');      //version 1.0.1, don't do version 1.1.0

//SET UP THE OAUTH OBJECT
var oauth = OAuth({
    consumer: consumer,
    signature_method: 'HMAC-SHA256'         //you can also use HMAC-SHA1 but HMAC-SHA256 is more secure (supposedly)
});

//SET UP THE REQUEST OBJECT
var request_data = {
    url: restlet_url,
    method: 'POST',
};

//GET THE AUTHORIZATION AND STICK IT IN THE HEADER, ALONG WITH THE REALM AND CONTENT-TYPE
var authorization = oauth.authorize(request_data, token);
var header = oauth.toHeader(authorization);
header.Authorization += ', realm="' + accountID + '"';
header['content-type'] = 'application/json';

//MAKE THE REQUEST
request({
    url: request_data.url,
    method: request_data.method,
    headers: header,
    json: {
        message: "test123"                  //this is your payload
    }
}, function(error, response, body) {
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log(body);
    }
});

If anybody has any problems with this code, leave a response and I’ll do my best to help.

Leave a Comment