Can anyone recommend a Python library for talking ...
# suitescript
c
Can anyone recommend a Python library for talking to Restlets? Or just some working Python code for making a request to a Restlet?
b
requests-oauthlib is a reasonable choice, or any of the other oauthlib based choices
c
Thank you very much. I'm working with a friend on a NS integration project. He's doing the work outside NS, so the first step is enabling him to make a successful request. This is his first experience of NS and he hates it already :)
Would you happen to have any working code?
b
no shareable code, the primary criteria you want when looking for library is support for HMAC-SHA256 and realms, which are both supported
c
ok, thank you
r
python code for connection with netsuite restlet using outh 1.0 Add the necessary details regarding tokens, secrets and restlet url and respective variables.
Copy code
from datetime import date
import time
import binascii
import hmac
from hashlib import sha256
import oauth2 as oauth
import requests

#BEGIN of input necessary details below
#your restlet url
url = ""
#access token and token secret
token = oauth.Token(key="",secret="")
#consumer key and secret
consumer = oauth.Consumer(key="", secret="")
http_method = "POST"
#netsuite realm
realm = ""
#ENd of input necessary details below

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
}

class SignatureMethod_HMAC_SHA256(oauth.SignatureMethod):
    name = 'HMAC-SHA256'
    def signing_base(self, request, consumer, token):
        if (not hasattr(request, 'normalized_url') or request.normalized_url is None):
            raise ValueError("Base URL for request is not set.")
        sig = (
            oauth.escape(request.method),
            oauth.escape(request.normalized_url),
            oauth.escape(request.get_normalized_parameters()),
        )
        key = '%s&' % oauth.escape(consumer.secret)
        if token:
            key += oauth.escape(token.secret)
        raw = '&'.join(sig)
        return key.encode('ascii'), raw.encode('ascii')

    def sign(self, request, consumer, token):
        """Builds the base signature string."""
        key, raw = self.signing_base(request, consumer, token)
        hashed = hmac.new(key, raw, sha256)
        # Calculate the digest base 64.
        return binascii.b2a_base64(hashed.digest())[:-1]

    req = oauth.Request(method=http_method, url=url, parameters=params)
    oauth.SignatureMethod_HMAC_SHA256 = SignatureMethod_HMAC_SHA256
    signature_method = oauth.SignatureMethod_HMAC_SHA256()
    req.sign_request(signature_method, consumer, token)
    header = req.to_header(realm)
    header_y = header['Authorization'].encode('ascii', 'ignore')
    header_x = {"Authorization": header_y, "Content-Type": "application/json"}
    today = date.today().strftime('%m/%d/%y') 
    myJson = {"lastUpdatedTimestamp":today}
    response = <http://requests.post|requests.post>(url, json=myJson, headers=header_x)
    if (response.status_code!=200) :
        raise Exception(response.content)
    else:
        data = response.json()
python code for connection with netsuite restlet using outh 1.0
c
Thank you very much both of you. (I hadn't opened Slack since battk replied so I didn't know there were more replies until just now)