Luke Collins
04/04/2024, 5:27 AMLuke Collins
04/04/2024, 5:28 AMimport time
import json
import math
import urllib
import random
import hashlib
import hmac
import base64
import requests
from flask import Flask, request, jsonify
from authlib.common.encoding import to_bytes
CONSUMER_KEY = "CONSUMER_KEY"
CONSUMER_SECRET = "CONSUMER_SECRET"
TOKEN_ID = "TOKEN_ID"
TOKEN_SECRET = "TOKEN_SECRET"
NETSUITE_ACCOUNT_ID = '1234567_SB1'
SIGN_METHOD = 'HMAC-SHA256'
OAUTH_VERSION = '1.0'
RESTLET_URL = "<https://1234567-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl>?"
def main():
webhook_payload = json.dumps({
"from": "0123456789",
"to": "0123456789",
"message": "message_sent",
"salesOrderId": "123456",
"msgref": "123456789"
})
response = forward_to_netsuite(webhook_payload)
print(response)
def getAuthNonce():
nonce_text = ''
length = 11
possible= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for i in range(length):
nonce_text += possible[math.floor(random.uniform(0, 1) * len(possible))]
return nonce_text
def getSignature(BASE_URL, HTTP_METHOD, OAUTH_NONCE, TIME_STAMP):
print(TIME_STAMP)
data = 'deploy=' + '1' + "&"
data += '&oauth_consumer_key' + "=" + CONSUMER_KEY + "&"
data += 'oauth_nonce' + "=" + OAUTH_NONCE + "&"
data += 'oauth_signature_method' + "=" + SIGN_METHOD + "&"
data += 'oauth_timestamp' + "=" + str(TIME_STAMP) + "&"
data += 'oauth_token' + "=" + TOKEN_ID + "&"
data += 'oauth_version' + "=" + OAUTH_VERSION
data += 'script=' + '1269'
signatureValue = HTTP_METHOD + '&' + urllib.parse.quote(BASE_URL, safe='~()*!.\'') + '&' + urllib.parse.quote(data,safe='~()*!.\'')
signatureKey = urllib.parse.quote(CONSUMER_SECRET, safe='~()*!.\'') + '&' + urllib.parse.quote(TOKEN_SECRET,safe='~()*!.\'')
print("signatureValue: " + signatureValue)
print("signatureKey: " + signatureKey)
shaData = hmac.new(to_bytes(signatureKey), to_bytes(signatureValue), hashlib.sha256)
base64EncodedData = base64.b64encode(shaData.digest())[:-1]
oauth_signature = base64EncodedData.decode('utf-8')
oauth_signature = urllib.parse.quote(oauth_signature, safe='~()*!.\'')
print("oauthsignature quote: " + oauth_signature)
return oauth_signature
def createHeader():
BASE_URL = RESTLET_URL
OAUTH_NONCE = getAuthNonce()
TIME_STAMP = round(time.time())
HTTP_METHOD = "POST"
oauth_signature = getSignature(BASE_URL, HTTP_METHOD, OAUTH_NONCE, TIME_STAMP)
OAuthHeader = 'OAuth '
OAuthHeader += 'realm=\"' + NETSUITE_ACCOUNT_ID + '\",'
OAuthHeader += 'oauth_consumer_key=\"' + CONSUMER_KEY + '\",'
OAuthHeader += 'oauth_token=\"' + TOKEN_ID + '\",'
OAuthHeader += 'oauth_signature_method="' + SIGN_METHOD + '",'
OAuthHeader += 'oauth_timestamp=\"' + str(TIME_STAMP) + '\",'
OAuthHeader += 'oauth_nonce=\"' + OAUTH_NONCE + '\",'
OAuthHeader += 'oauth_version=\"1.0\",'
OAuthHeader += 'oauth_signature=\"' + oauth_signature + '\"'
print(oauth_signature)
print(OAUTH_NONCE)
print(OAuthHeader)
headers = {
"Content-Type": "application/json",
"Authorization": OAuthHeader,
"Host": "<http://1234567-sb1.restlets.api.netsuite.com|1234567-sb1.restlets.api.netsuite.com>",
"Cache-Control": "no-cache",
"Cookie": "NS_ROUTING_VERSION=LAGGING"
}
return headers
def forward_to_netsuite(data):
headers = createHeader()
response = requests.request("POST", RESTLET_URL, headers=headers, data=data)
return response.json(), response.status_code
if __name__ == '__main__':
main()
Luke Collins
04/04/2024, 5:31 AMOAuth realm="1234567_SB1",oauth_consumer_key="CONSUMER_KEY",oauth_token="TOKEN_KEY",oauth_signature_method="HMAC-SHA256",oauth_timestamp="1712208251",oauth_nonce="8wU7UP8pr7V",oauth_version="1.0",oauth_signature="w3Br9xx%2FBNA6A6wnccVBFBPEJT1II9aN0LbHTExdVY8"
battk
04/04/2024, 5:38 AMbattk
04/04/2024, 5:38 AMLuke Collins
04/04/2024, 5:42 AMbattk
04/04/2024, 5:42 AMbattk
04/04/2024, 5:53 AMEric B
04/04/2024, 6:34 AMEric B
04/04/2024, 6:35 AMEric B
04/04/2024, 6:35 AMNElliott
04/04/2024, 8:50 AMimport requests
from requests_oauthlib import OAuth1
Hopefully it gives you what you need (albeit my example is using SuiteTalk).Luke Collins
04/04/2024, 9:57 AMLuke Collins
04/04/2024, 10:03 AMLuke Collins
04/04/2024, 10:04 AMimport requests
import json
from requests_oauthlib import OAuth1
RESTLET_URL = "<https://1234567-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=1269&deploy=1>"
CLIENT_KEY = "C_KEY"
CLIENT_SECRET = "C_SECRET"
TOKEN_KEY = "T_KEY"
TOKEN_SECRET = "T_SECRET"
http_method = "POST"
realm = "1234567_SB1"
def main():
oauth = OAuth1(CLIENT_KEY, CLIENT_SECRET, TOKEN_KEY, TOKEN_SECRET,
signature_method="HMAC-SHA256", realm=realm)
payload = {
"from": "0123456789"
}
json_payload = json.dumps(payload)
headers = {
"prefer": "transient",
"Content-Type": "application/json",
"Cookie": "NS_ROUTING_VERSION=LAGGING"
}
response = requests.request("POST", url=RESTLET_URL, auth=oauth, headers=headers, data=json_payload)
print(response.content, response)
if __name__ == '__main__':
main()
battk
04/05/2024, 4:46 AMbattk
04/05/2024, 4:47 AMRESTLET_URL = "<https://1234567-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=1269&deploy=1>"
CLIENT_KEY = "C_KEY"
CLIENT_SECRET = "C_SECRET"
TOKEN_KEY = "T_KEY"
TOKEN_SECRET = "T_SECRET"
http_method = "POST"
realm = "1234567_SB1"
battk
04/05/2024, 4:57 AMLuke Collins
04/05/2024, 5:50 AM