0
votes

Error: "Authorization Required - You are not authorized to access the requested resource. Check the supplied credentials (e.g., username and password)."

Using the same exact headers and URL, I am successfully able to make the request get through via Postman and Powershell. But when doing the call via SuiteScript, I get the auth error. I am thinking it may have something to do with me constructing the headers.

Here is the code I used via NetSuite Debugger:

require(['N/https', 'N/encode'], function(https, encode) {
    
    function fetchCSVdata() {

        var authObj = encode.convert({
            string : "username:password",
            inputEncoding : encode.Encoding.UTF_8,
            outputEncoding : encode.Encoding.BASE_64
        });
        
        var psswd = 'Basic ' + authObj;

        var headerObj = {'Authorization' : psswd};
        
        var response = https.get({
            url: 'https://<bleep>.pbcs.us6.oraclecloud.com/interop/rest/11.1.2.3.600/applicationsnapshots/DemandPlan_ExportItemPlan.csv/contents',
            headers: headerObj
        });
        
        return response.body;

    };
    
    var x = fetchCSVdata();
    log.debug("error", x);
});
1
Can you get the server to log the requests (including headers) and then compare the difference between the two requests?MT0
That was weird, when I specifically enter a base64 string on the Authorization header, it worked. What's weird is when I try to encode the credentials into base64 using Netsuite's native encoding and it us, that's where it fails. I was able to do this by comparing the headers from the server log. Thank you!Angelo Raphael Garcia

1 Answers

0
votes

Looking at some working code of mine it is different than yours but I don't see the error.

    var authstring = encode.convert({string: 'username:password',
                    inputEncoding: encode.Encoding.UTF_8,
                    outputEncoding: encode.Encoding.BASE_64});
    var headerObj = {Authorization: 'Basic '+ authstring };
    var response = https.get({url: 'https://webservices.XXX.com', headers: headerObj});