Unable to pass authentication information to NetSuites REST interface using Python

2024/9/16 23:10:35

I've been trying to get a NetSuite Restlet to work with Python 3.3 using urllib, but I can't seem to get the authorization to take and continually return a urllib.error.HTTPError: HTTP Error 401: Authorization Required error.

Where am I going wrong with authentication?

My test code is as follows:

import urllib.requesturl = 'https://rest.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=15&recordtype=salesorder&id=123456789'authorization = 'NLAuth nlauth_account=111111,[email protected],nlauth_signature=password,nlauth_role=3' req = urllib.request.Request(url)
req.add_header('Authorization', authorization)
req.add_header('Content-Type','application/json')
req.add_header('Accept','*/*')
response = urllib.request.urlopen(req)
the_page = response.read()

For reference, NetSuite's REST help was used to build the authorization string as well as the Python docs on urllib located here

--EDIT--

The header that is passed (and works) via REST Console appears as follows:

Accept: application/json
Authorization: NLAuth nlauth_account=111111,[email protected],nlauth_signature=password,nlauth_role=3
Connection: keep-alive
Content-Type: application/xml
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36

headers output from Python appear as:

{'Content-type': 'application/xml', 'Accept': 'application/json', 'Authorization': 'NLAuth nlauth_account=111111,[email protected],nlauth_signature=password,nlauth_role=3'}

still not sure why it's not working....

Answer

Issue was NetSuite related (an issue with roles): Code below yeilds a proper response:

import urllib.request
try:   url = 'https://rest.netsuite.com/app/site/hosting/restlet.nl?script=787&deploy=1&recordtype=salesorder&id=8111437'authorization = 'NLAuth nlauth_account=111111,[email protected],nlauth_signature=password,nlauth_role=correctRole' req = urllib.request.Request(url)req.add_header('Authorization', authorization)req.add_header('Content-Type','application/xml')req.add_header('Accept','application/json')  response = urllib.request.urlopen(req)print(response.read())
except IOError as e:print("EXCEPTION OCCURRED--------------------")print("I/O error: {0}".format(e))print(e.headers)print(e.headers['www-authenticate'])

In my case, the original role did not have access to the record. The confusion was I was expecting an error of

error code: INVALID_ROLE
error message:Your role does not give you permission to view this page.

to be returned, not an authentication required which made me suspect the header was missing data.

https://en.xdnf.cn/q/72613.html

Related Q&A

letsencrypt failed with ImportError: No module named interface

Im using Amazon linux, and I followed some steps for using letsencrypt that easily found in google search, but all it fails with:Error: couldnt get currently installed version for /root/.local/share/le…

ModuleNotFoundError: No module named skimage.util.montage

Im trying to import montage2d module from scikit-image:from skimage.util.montage import montage2dBut this error popped up:ModuleNotFoundError: No module named skimage.util.montageIm pretty sure I insta…

upgrading python module within code

My question relates to this question: Installing python module within code, but involves upgrading the module.Ive triedpackages=[apscheduler,beautifulsoup4,gdata]def upgrade(packages):for package in pa…

Django - 403 Forbidden CSRF verification failed

I have a contact form in Django for my website and when I was testing it locally it was working fine but now when I try to submit my contact form "live" it always comes up with 403 Forbidden …

Python model object validation

Im writing an interface to be used by two applications. This interface should use some DoSomethingRequest and DoSomethingResponse classes to do the communication.Is there any library that does some mod…

Cassandra 1.2 inserting/updating a blob column type using Python and the cql library

IntroI have a blob column on a Cassandra 1.2 column family, the table is defined as follows:CREATE TABLE objects (id text,obj blob,PRIMARY KEY (id) );The problem:The problem is that when I…

Using python with subprocess Popen

I am struggling to use subprocesses with python. Here is my task:Start an api via the command line (this should be no different than running any argument on the command line) Verify my API has come …

ipdb, multiple threads and autoreloading programs causing ProgrammingError

I am using ipdb debugger to debug multithreaded web applications locally (Django, Plone). Often ipdb seems to get confused because of the autoreload which happens when I am on the debug prompt. The res…

How to turn off logging buffer to get logs in real time with python command line tool?

I have a command line tool which produces plenty of logs. I want these logs to be sent to stdout as soon as theyre made. Right now, the program finishes everything (which can take several minutes), and…

How to tell if process is responding in Python on Windows

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I cant quite figure out how to do this.