<@U5375UUJ2> you need to have oauth and cryptojs i...
# suitescript
h
@stalbert you need to have oauth and cryptojs imported
Copy code
var userName = runtime.getCurrentUser().name;
            var headers = oauth.getHeaders({
                url: config.IMPORT_ORDERS_RESTLET_URL,
                method: 'POST',
                tokenKey: config.TBA_TOKENS[userName]['tokenid'],
                tokenSecret: config.TBA_TOKENS[userName]['tokensecret']
            });
            var body = {
                csvfileid: csvFileId,
                sportscvfileid: sportsFileId
            };
            headers['Content-Type'] = 'application/json';
            var restResponse = <http://https.post|https.post>({
                url: config.IMPORT_ORDERS_RESTLET_URL,
                headers: headers,
                body: JSON.stringify(body)
            });
s
Nice, where/how is oauth declared? or in other words, does NS like the default cryptojs and oauth1.o libs that work in SS1?
h
give me a minute ...
s
no rush - good to see someone else has already been here, done that 🙂
h
Copy code
define(['N/runtime', 'N/error', 'N/file', 'N/email', 'N/search', 'N/format','N/task','N/https', '../lib/oauth', '../lib/cryptojs', 'N/record', '../lib/ProcessingLogModule','../config/GrouponProcessingConfigurationModule', '../lib/DebugHelperModule'],
function(runtime, error, file, email, search, format, task, https, oauth, cryptojs, record, processlog, config, debug) {
this header is correct
s
and the oauth/cryptojs are the same lib files used in SS1?
h
what do you mean in SS1 ?
My script is written in SS 2.0
s
aye, I was wondering if you were using different versions of the crypto/oauth libs for SS2
those two you just linked looked like the same ones that worked in SS1
h
I have both in "lib" directory
'../lib/oauth'
s
understood. I'll give it a try
thanks for paving the way!
👍 1
h
lib is just a directory in Suitescript folder where I keep all libraries that I use
I like to have things organized so there is no need to copy them to each project folder as I keep all projects in separate folders
and config is just a configuration module
s
yes, sound practices all round
h
let me know if that worked for you
j
@Hubert Thanks for posting that, is your consumer key defined in the oauth library?
👍 1
s
yes @Hubert thanks again
👍 1
one note, you are able to successfully use that code without setting the 'realm' property?
NS used to fail if you didn't include the 'realm' property - maybe they've loosened that restriction?
h
Copy code
function() {
		
		const GROUPON_PROCESS_FOLDER_ID = 14829;
		const GROUPON_COMBINED_FILES_FOLDER_ID = 14838;
		const PICK_TICKET_FOLDER_ID = 20812;
		const CSV_FILES_DESTINATION_FOLDER_ID = 14828;
		const CSV_IMPORT_MAPPING_ID = 'custimport_ggautofulfill';
		const CSV_IMPORT_SPORT_MAPPING_ID = 'custimport_ggsportautofulfill'; 
		const IMPORT_ORDERS_RESTLET_URL = '<https://restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=217&deploy=1>'; //REST Endpoint URL
		const SEND_DATA_FOR_LABELS_RESTLET = '<https://restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=218&deploy=1>'; 
		const BATCH_SIZE = 27;
		const TBA_TOKENS = {
				'hubert' : { 'tokenid' : '7576a20af1711199d211142', 
					'tokensecret' : '7576a20af1711199d211142' 
				},
				'kelly' : { 'tokenid' : '26195ca2487c200f4b8a537a', 
					'tokensecret' : '26195ca2487c200f4b8a537a' 
				}
		};
		
    return {
    	TBA_TOKENS : TBA_TOKENS,
    	GROUPON_PROCESS_FOLDER_ID : GROUPON_PROCESS_FOLDER_ID,
    	PROCESS_GROUPON_MR_SCRIPT : PROCESS_GROUPON_MR_SCRIPT,
    	IMPORT_ORDERS_RESTLET_URL : IMPORT_ORDERS_RESTLET_URL,
    	GROUPON_COMBINED_FILES_FOLDER_ID : GROUPON_COMBINED_FILES_FOLDER_ID,
    	PICK_TICKET_FOLDER_ID : PICK_TICKET_FOLDER_ID,
    	CSV_FILES_DESTINATION_FOLDER_ID : CSV_FILES_DESTINATION_FOLDER_ID,
    	CSV_IMPORT_MAPPING_ID : CSV_IMPORT_MAPPING_ID,
    	CSV_IMPORT_SPORT_MAPPING_ID : CSV_IMPORT_SPORT_MAPPING_ID,
    	SEND_DATA_FOR_LABELS_RESTLET : SEND_DATA_FOR_LABELS_RESTLET,
    	BATCH_SIZE : BATCH_SIZE,
    	
    };
    
});
that's my config module but I removed vulnerable data
what do you mean realm property ?
that code is working every day 😉
j
In external connections to NetSuite I still need to make sure realm=ACOUNTNUMBER is in the authorization header
h
I am calling this restlet from M/R script
j
So since this is in context of NetSuite to NetSuite perhaps realm, or consumer key isn't required
h
realm is in url tho'
s
John, probably right re: realm
j
@Hubert just wanted to clarify you created your own oauth library that's built on top of the oauth git link you provided correct? The method call in your sample isn't part of ddo/oauth-1.0a so I figure you put a wrapper around it where you also have your consumer key specified in that library.
h
@JohnnyC I am sorry I was very busy and could not reply, I did it long time ago and I quickly grabbed that snippet and links to those 2 libraries , here is oauth and crypto from filecabinet that is being used
oauth.js
j
Thanks for taking the time much appreciated.
h
@stalbert now you should have everything what you need
s
thanks @Hubert did you adjust the OAuth.js library to be AMD compatible yourself? I am presuming so since you introduced the './secret` dependency?
get a load of this - I had a colleague mentioned they have this working using a non-AMD OAuth.js lib....
h
@stalbert no I am not the author of those scripts (oauth and cryptojs) and as far as I remember someone posted it here or links to them, I used them to implement my solution, initially I wrongly posted those links to github (I thought that those were the ones that I have in filecabinet), secret.js is just a module that holds consumer key and secret
@stalbert Did you make it working ?
s
I'm trying to solve a mystery about how NS AMD handles global functions in modules
I have another version of OAuth which seemed to work even though it is not a proper AMD module
h
these 2 that I posted here (not the github links) work for me
for SS2 code
s
OK. Since CryptoJS has AMD support right out of the box I'll stick with the vanilla flavor of that, and I'm thinking it best to just use a similar header around the OAuth code as well