raghav
04/13/2023, 7:52 AMraghav
04/13/2023, 12:38 PMimport requests
import logging
from pathlib import Path
import datetime
import jwt # PyJWT
GRANT_TYPE = "client_credentials"
CLIENT_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
CLIENT_ID = "CONSUMER KEY / CLIENT ID from netsuite"
TOKEN_ENDPOINT_URL = "https://<acccountid>.<http://suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token|suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token>"
CONNECT_ENDPOINT_URL = "https://<acccountid>.<http://suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token|suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token>"
CERTIFICATE_ID = "<generaed kid>"
private_key = "<privatekey from openssl>"
SCOPES = ["restlets", "rest_webservices"]
jwtheader = {
"alg": "RS256",
"typ": "JWT",
"kid": CERTIFICATE_ID
}
def main():
now = datetime.datetime.now()
payload_data = {
'iss': CLIENT_ID,
'scope': SCOPES,
'aud': CONNECT_ENDPOINT_URL,
'iat': now.timestamp(),
'exp': now + datetime.timedelta(hours=1),
}
jwt_assertion = jwt.encode(payload= payload_data, key=private_key, headers=jwtheader)
print("jwt_assertion",jwt_assertion)
data = {
'grant_type': GRANT_TYPE,
'client_assertion_type': CLIENT_ASSERTION_TYPE,
'client_assertion': jwt_assertion,
}
response = <http://requests.post|requests.post>(TOKEN_ENDPOINT_URL, data=data) #invalid login attemt in the response
data = response.json()
print("data",data)
access_token = data["access_token"]
if response.ok:
access_token = response.json().get('access_token')
print('Access token:', access_token)
else:
print('Error requesting access token:', response.text)
# Call RESTlet using access token
restlet_url = "https://<accountid>.<http://restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=<scriptid>deploy=<deploymentid|restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=<scriptid>deploy=<deploymentid>>"
payload = {
"RequestType": "<xxx>",
"RequestData": "<xxx>"
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token
}
restletRsponse = <http://requests.post|requests.post>(restlet_url, data=payload, headers=headers)
print("restletRsponse",restletRsponse)
if __name__ == '__main__':
main()
Selcuk Dogru
04/13/2023, 3:13 PMraghav
04/13/2023, 4:28 PMraghav
04/13/2023, 5:09 PMSelcuk Dogru
04/13/2023, 6:14 PMraghav
04/14/2023, 6:05 AMSelcuk Dogru
04/14/2023, 12:29 PMpayload = {
'iss': CLIENT_ID,
'scope': SCOPES,
'aud': TOKEN_ENDPOINT_URL,
'iat': int(now.timestamp()),
'exp': int((now + datetime.timedelta(hours=1)).timestamp())
}
raghav
04/14/2023, 12:43 PM