无法使用Python将身份验证信息传递到NetSuite的REST接口 [英] Unable to pass authentication information to NetSuite's REST interface using Python

查看:125
本文介绍了无法使用Python将身份验证信息传递到NetSuite的REST接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使用urllib使NetSuite Restlet与Python 3.3配合使用,但是我似乎无法获得授权并连续返回urllib.error.HTTPError: HTTP Error 401: Authorization Required错误.

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?

我的测试代码如下:

import urllib.request

url = 'https://rest.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=15&recordtype=salesorder&id=123456789'

authorization = 'NLAuth nlauth_account=111111,nlauth_email=email@email.com,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()

作为参考,NetSuite的REST帮助用于在

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

-编辑-

通过REST控制台传递(和起作用)的标题如下:

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

Accept: application/json
Authorization: NLAuth nlauth_account=111111,nlauth_email=user@email.com,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

Python的

标头输出显示为:

headers output from Python appear as:

{'Content-type': 'application/xml', 'Accept': 'application/json', 'Authorization': 'NLAuth nlauth_account=111111,nlauth_email=email@email.com,nlauth_signature=password,nlauth_role=3'}

仍然不确定为什么它不起作用....

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

推荐答案

问题与NetSuite有关(角色相关的问题):下面的代码提出了正确的答案:

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,nlauth_email=email@email.com,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.

这篇关于无法使用Python将身份验证信息传递到NetSuite的REST接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆