Skip to content

Instantly share code, notes, and snippets.

@axilaris
Created October 6, 2021 09:07
Show Gist options
  • Save axilaris/6ecc58e43895dba17ce0ca1b8ce25d9a to your computer and use it in GitHub Desktop.
Save axilaris/6ecc58e43895dba17ce0ca1b8ce25d9a to your computer and use it in GitHub Desktop.
import oauth2 as oauth
import json
import requests
import time
url = "https://1234567-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=553&deploy=1"
token = oauth.Token(key="1638cc7b433261eeed83462336e4fabfd34442cad601dfa486f98752ebc40554",
secret="273dfacad10fc828994f66e7c16d45247b7420780a15801d5bb8541095209935")
consumer = oauth.Consumer(key="911d0e2e75cb5a7105f0326d11bc4ba36985113a4add262e377aae427e367ff2",
secret="615b96eb91be2b7f32987f2570245bc5eb830130c3a713c5ea25866e213597cb")
http_method = "POST"
realm = "1234567_SB1" # NetSuite account ID
# the JSON data to be sent to the RESTlet
payload = {
"name":"value",
"foo":"bar",
"duck":"hunt",
}
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_SHA1()
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(payload))
conn = requests.post(url, headers=headerx)
print("Result: " + conn.text)
print(conn.headers)
@axilaris
Copy link
Author

axilaris commented Oct 6, 2021

% python3 netsuitetest.py
{'Authorization': b'OAuth realm="1234567_SB1", oauth_version="1.0", oauth_nonce="66548458", oauth_timestamp="1633509574", oauth_token="1638cc7b433261eeed83462336e4fabfd34442cad601dfa486f98752ebc40554", oauth_consumer_key="911d0e2e75cb5a7105f0326d11bc4ba36985113a4add262e377aae427e367ff2", oauth_body_hash="2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D", oauth_signature_method="HMAC-SHA1", oauth_signature="NftJ3TVvbxTUc3vkBqQ2970CcuU%3D"', 'Content-Type': 'application/json'}
Result: {"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}
{'WWW-Authenticate': 'OAuth realm="1234567_SB1"', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '84', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Wed, 06 Oct 2021 08:39:36 GMT', 'Connection': 'close', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.84eece17.1633509575.161835d7'}

@axilaris
Copy link
Author

axilaris commented Oct 6, 2021

"message" : "Invalid login attempt."

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