简体   繁体   中英

Netsuite REST Web Services API GET Request, Outside of Postman

Hi Stack Overflow community, thanks in advance for your help:

Question for Community: Instead of reinventing the oauth wheel, are there any tips / best practices for creating my own oauth signature/nonce within my python GET requests for Netsuite's new API (REST Web Services; see below for context of question)? It seems that other folks who have been successful at this have done it through trial and error which is my plan as well, but ideally I'd like to have fewer errors and again, not reinvent the wheel. Any tips, tricks, ideas are greatly welcome. See context below

What: Attempting to make a GET request using Netsuite's brand new REST API (REST Web Services). This is a different API than their SOAP/ RESTlets.

How: Through writing Python script in Visual Studio Code. I am successful at making the request in Postman. I copied the code into Visual Studio Code that Postman used to make the successful GET request and received a 401 response (see below).

Problem Encountered: I receive a 401 response, invalid login. There is no official Netsuite documentation on how make a successful interaction with this new REST API outside of Postman, so after reading through StackOverflow and other blogs/publications it seems that I need to create my own oauth_signature, oauth_timestamp, and oauth_nonce.

Postman GET Request Code:

import requests

url = "https://123456-sb1.suitetalk.api.netsuite.com/services/rest/query/v1/workbook/custworkbook12345/result"

payload = {}
headers = {
  'Authorization': 'OAuth realm="123456_SB1",oauth_consumer_key="123456789101112131415",oauth_token="123456789101112131415",oauth_signature_method="HMAC-SHA256",oauth_timestamp="123456789",oauth_nonce="123456789",oauth_version="1.0",oauth_signature="123456789101112131415"',
  'Cookie': 'NS_ROUTING_VERSION=LAGGING'
}

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

print(response.text.encode('utf8'))


Thanks in advance!

Thanks to Josh's recommendation (see comments to my original question) I successfully used oauthlib's oauth1 client to send a request in Visual Studio Code. The nonce and signature look a little different than what Postman shows in their code snippet, but it did work. For anyone attempting the same thing with Netsuite's REST Web Services I suggest going this route.

My code that sent a successful GET request:

import requests
import oauthlib.oauth1
import json


url = "https://12345-sb1.suitetalk.api.netsuite.com/services/rest/query/v1/dataset/custdataset1/result"

payload = {}

client = oauthlib.oauth1.Client('consumer key', client_secret='12345',
    resource_owner_key='12345', resource_owner_secret='12345', realm='12345_SB1',signature_method="HMAC-SHA256")
url, headers, body = client.sign('https://4635201-sb4.suitetalk.api.netsuite.com/services/rest/query/v1/dataset/custdataset1/result')


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

print(response.text.encode('utf8'))

A Few Additional Helpful Notes -

  • I'm testing this in Netsuite Sandbox, hence the realm "12345_SB1". If you aren't in sandbox you shouldn't need the underscore SB. Just use your account ID.
  • I'm pulling Netsuite Analytics Report, which at this time is still in beta for the new API (REST Web Services).
  • I used the Python oauthlib that Josh recommended and I recommend you do the same, link here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM