4

I am trying to generate an oauth_signature for an API call. Using Postman, I have generated the code as follows

import requests
import json

url = "https://xxxxxxxxxxxx"

payload = json.dumps({
"key1": "value",
"Key2": "value2"
})

headers = {
  'Authorization': 'OAuth realm="xxxxxxxx",
   oauth_consumer_key="xxxxxxxxxxxxxxx",
   oauth_token="xxxxxxxxxxxx",
   oauth_signature_method="HMAC-SHA256",
   oauth_timestamp="1628552790",
   oauth_nonce="xxxxxxxx",
   oauth_version="1.0",
   oauth_signature="xxxxxxxxxxxxxxx"',
  'Content-Type': 'application/json',
  'Cookie': 'NS_ROUTING_VERSION=LAGGING'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

I want to use Python to generate the oauth_signature as Postman generates this auomatically without much documentation as to how

GeneralCTR
  • 41
  • 3

1 Answers1

1

Making a Oauth 1.0 request needs to generate a oauth signature, in my case I am using SHA256 for Netsuite, so there is method for this based in timestamp and random nonce. I hope this can be usefull for you:

import time #To generate the OAuth timestamp
import urllib.parse #To URLencode the parameter string
import hmac #To implement HMAC algorithm
import hashlib #To generate SHA256 digest
from base64 import b64encode #To encode binary data into Base64
import binascii #To convert data into ASCII
import requests #To make HTTP requests
import random


## PARAMS ##
oauth_consumer_key = 'xxxx'
oauth_signature_method = 'HMAC-SHA256'
oauth_version = '1.0'
account = 0000
consumer_secret = "xxxx"
access_token = "xxxx"
token_secret = "xxxx"
## PARAMS ##

method = 'POST'
url = 'https://'+str(account)+'.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql'
body = "{\n\t\"q\": \"SELECT id, companyName, email, dateCreated FROM customer WHERE dateCreated >= '01/01/2022' AND dateCreated < '12/12/2022'\"\n}"

oauth_timestamp = str(int(time.time()))
oauth_nonce = ''.join(random.choices("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", k=11))

def create_parameter_string(oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version, token_id):
    parameter_string = ''
    #parameter_string = parameter_string + 'grant_type=' + grant_type
    parameter_string = parameter_string + 'oauth_consumer_key=' + oauth_consumer_key
    parameter_string = parameter_string + '&oauth_nonce=' + oauth_nonce
    parameter_string = parameter_string + '&oauth_signature_method=' + oauth_signature_method
    parameter_string = parameter_string + '&oauth_timestamp=' + oauth_timestamp
    parameter_string = parameter_string + '&oauth_token=' + token_id
    parameter_string = parameter_string + '&oauth_version=' + oauth_version
    return parameter_string

parameter_string = create_parameter_string(oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version,access_token)
encoded_parameter_string = urllib.parse.quote(parameter_string, safe='')

encoded_base_string = method + '&' + urllib.parse.quote(url, safe='')
encoded_base_string = encoded_base_string + '&' + encoded_parameter_string

signing_key = consumer_secret + '&' + token_secret

def create_signature(secret_key, signature_base_string):
    encoded_string = signature_base_string.encode()
    encoded_key = secret_key.encode()
    temp = hmac.new(encoded_key, encoded_string, hashlib.sha256).hexdigest()
    byte_array = b64encode(binascii.unhexlify(temp))
    return byte_array.decode()

oauth_signature = create_signature(signing_key, encoded_base_string)
encoded_oauth_signature = urllib.parse.quote(oauth_signature, safe='')

headers = {
    'Content-Type': 'text/plain',
    'prefer':'transient',
    'Authorization': 'OAuth realm="{0}",oauth_consumer_key="{1}",oauth_token="{2}",oauth_signature_method="{3}",oauth_timestamp="{4}",oauth_nonce="{5}",oauth_version="{6}",oauth_signature="{7}"'.format(
        str(account),oauth_consumer_key,access_token,oauth_signature_method, oauth_timestamp ,oauth_nonce,oauth_version ,encoded_oauth_signature)
}

print(headers)

response = requests.post(url, data=body, headers=headers)
print(response.text)
landrady
  • 11
  • 2