Skip to content

Instantly share code, notes, and snippets.

@axilaris
Created October 12, 2021 15:39
Show Gist options
  • Save axilaris/4386c3537d04737d3775c156562b7545 to your computer and use it in GitHub Desktop.
Save axilaris/4386c3537d04737d3775c156562b7545 to your computer and use it in GitHub Desktop.
import oauth2 as oauth
import json
import requests
import time
import hashlib
import hmac
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_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(payload))
conn = requests.post(url, headers=headerx)
print("Result: " + conn.text)
print(conn.headers)
@axilaris
Copy link
Author

the success output:

Result: org.mozilla.javascript.Undefined@fc0db98
{'X-N-OperationId': 'f41235b5-07b4-4958-b34b-54cddceaa146', 'NS_RTIMER_COMPOSITE': '1158157820: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': '40', 'P3P': 'CP="CAO PSAa OUR BUS PUR"', 'Vary': 'User-Agent', 'Date': 'Thu, 07 Oct 2021 09:27:58 GMT', 'Connection': 'keep-alive', 'Set-Cookie': 'NS_ROUTING_VERSION=LAGGING; path=/', 'Akamai-GRN': '0.8feece17.1633598877.2c81bc9'}

@axilaris
Copy link
Author

axilaris commented Oct 12, 2021

@bigadd88
Copy link

I'm super new to Python, like just today. I've got the oauth library added. But I get this error when trying to run my python file from my command prompt.

Traceback (most recent call last):
File "create_vendor_bill.py", line 10, in
token = oauth.Token(key="6653fcbebf7a5366e6385937f512b8b0f4aabd4f5a41d894b3f9ebd9032fdd3b",
AttributeError: 'module' object has no attribute 'Token'

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