can any1 provide sample code to call a netsuite re...
# suitetalkapi
r
can any1 provide sample code to call a netsuite reslet using Oauth2.0 in python? Having issue with getting the bearer token from Netsuite. Not exactly able to debug the issue is.
Can someone please help me debug this?
Copy code
import 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()
s
do you need to send algorithm during jwt.encode?
r
Yes, as far as I have seen it's required. And even in postman we are doing the same. And in other integration where the backend is in Java, we are doing the same..
If anyone has a working code please do provide.
s
what I meant was on jwt.encode you do not pass algorithm parameter, maybe it will help
r
tried without passing it as well This is what I am refencing https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/article_0907012935.html
s
it seems payload is wrong. correct should be:
Copy code
payload = {
        'iss': CLIENT_ID,
        'scope': SCOPES,
        'aud': TOKEN_ENDPOINT_URL,
        'iat': int(now.timestamp()),
        'exp': int((now + datetime.timedelta(hours=1)).timestamp())
    }
r
Having a working code now. Thank you for spending your time on it. And beside the payload there were a few more issues. But you are correct time was one of the issue there.
👍 2