Netsuite OAuth Not Working

后端 未结 2 1013
广开言路
广开言路 2021-01-03 04:40

I\'ve tried implementing Netsuite\'s OAuth Example, as illustrated here: https://netsuite.custhelp.com/app/answers/detail/a_id/42165. I\'ve posted it directly below so you

2条回答
  •  Happy的楠姐
    2021-01-03 05:07

    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 oauth-1.0a@1.0.1
    

    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.

提交回复
热议问题