Skip to content

Instantly share code, notes, and snippets.

@axilaris
Last active November 9, 2023 07:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axilaris/428e63e5ff107d212fbcc07c5bdbce7a to your computer and use it in GitHub Desktop.
Save axilaris/428e63e5ff107d212fbcc07c5bdbce7a to your computer and use it in GitHub Desktop.
import oauth2 as oauth
import json
import requests
import time
import hashlib
import hmac
import requests_async as async_requests
url = "https://5008903-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=978&deploy=1"
token = oauth.Token(key="d0c58f511e12345678c3e969e09e0ae1d967869762a354523494f0b953e9",
secret="8a8536e0d2faaa123455678b9b3745b9cf71035c08ed1c7034423423c70ef")
consumer = oauth.Consumer(key="117123345674ffb0b98440badb57205b3542234832cbdbabca0",
secret="c7db9a1a2fa42dfe0123445677b6361fbafb7bdb6422344227125dd")
http_method = "POST"
realm = "5008903_SB1" # NetSuite account ID
payload1 = {
"activity_type":1,
"timestamp":"2021-05-01T06:07:47.443282Z",
"date":"2021-05-01T06:07:47.443282Z",
"employee_personal_id":"AAAA",
"company_name":"AAAA",
}
payload2 = {
"activity_type":1,
"timestamp":"2021-05-01T06:07:47.443282Z",
"date":"2021-05-01T06:07:47.443282Z",
"employee_personal_id":"BBBB",
"company_name":"BBBBB",
}
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': str(int(time.time())),
'oauth_token': token.key,
'oauth_consumer_key': consumer.key
}
req = oauth.Request(method=http_method, url=url, parameters=params)
signature_method = oauth.SignatureMethod_HMAC_SHA256()
req.sign_request(signature_method, consumer, token)
header = req.to_header(realm)
headery = header['Authorization'].encode('ascii', 'ignore')
headerx = {"Authorization": headery, "Content-Type": "application/json"}
print(headerx)
conn = requests.post(url, headers=headerx, data=json.dumps(payload1))
print("Result: " + conn.text)
print(conn.headers)
conn = requests.post(url, headers=headerx, data=json.dumps(payload2))
print("Result: " + conn.text)
print(conn.headers)
{'Authorization': b'OAuth realm="5504997_SB1", oauth_version="1.0", oauth_nonce="87811756", oauth_timestamp="1635417611", oauth_token="d0c58f511e12345678c3e969e09e0ae1d967869762a354523494f0b953e9", oauth_consumer_key="117123345674ffb0b98440badb57205b3542234832cbdbabca0", oauth_body_hash="2jmj7l5rSw0yVb%2FvlWAYeqweq%2FYBwk%3D", oauth_signature_method="HMAC-SHA256", oauth_signature="Apx76mZiSjKtF6WjLweqweqwRNV4KF1VEZAfURe8%3D"', 'Content-Type': 'application/json'}
Result: {"success":true}
{'X-N-OperationId': '42656b12-5874-4870-b958-631e8a298ecd', 'NS_RTIMER_COMPOSITE': '331252041:706172746E6572733031312E70726F642E6475622E6E65746C65646765722E636F6D:80', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Type': 'application/json;charset=utf-8', 'Pragma': 'No-Cache', 'Cache-Control': 'No-Cache', 'Expires': '0', 'Content-Length': '17', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Thu, 28 Oct 2021 10:40:11 GMT', 'Connection': 'keep-alive', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.57561b3a.1635417611.239d029f'}
Result: {"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}
{'WWW-Authenticate': 'OAuth realm="5008903_SB1"', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '84', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Thu, 28 Oct 2021 10:40:12 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.57561b3a.1635417611.239d0735'}
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define(["N/log", "N/record"], function (log, record) {
function post(context) {
return JSON.stringify(createCustomRecord(context));
}
function createCustomRecord(context) {
let success = true;
try {
let custRec = record.create({
type: "customrecordlog_time",
isDynamic: true,
});
//Set one or more fields here
custRec.setValue({
fieldId: "custrecorddatetime",
value: new Date(context.date),
});
custRec.setValue({
fieldId: "custrecordchecktype",
value: context.activity_type,
});
custRec.setValue({
fieldId: "custrecord3693",
value: new Date(context.timestamp),
});
custRec.setValue({
fieldId: "custrecord4017",
value: context.employee_id,
});
custRec.setValue({
fieldId: "custrecord4018",
value: context.company_id,
});
custRec.save();
} catch (e) {
log.error("Error creating record", e);
success = false;
}
return { success: success };
}
return {
post: post,
};
});
@axilaris
Copy link
Author

notice in output, it was success and then after the next request it is INVALID_LOGIN_ATTEMPT.

if I were to run this without creating the request twice, it will work each time I call it. But if in a process it calls twice or more, the 2nd request and so forth will have INVALID_LOGIN_ATTEMPT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment