5

我一直在尝试让 Ne​​tSuite Restlet 与 Python 3.3 一起使用urllib,但我似乎无法获得授权并不断返回urllib.error.HTTPError: HTTP Error 401: Authorization Required错误。

我在哪里进行身份验证?

我的测试代码如下:

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 帮助用于构建授权字符串以及位于此处的 urllib 上的 Python 文档

- 编辑 -

通过 REST 控制台传递(和工作)的标头如下所示:

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 的标头输出显示为:

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

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

4

1 回答 1

8

问题与 NetSuite 相关(角色问题):下面的代码会产生正确的响应:

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'])

就我而言,原始角色无权访问记录。混乱是我期待一个错误

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

要返回,而不是需要身份验证,这使我怀疑标头缺少数据。

于 2013-09-24T20:42:17.573 回答