无法使用Python将身份验证信息传递到NetSuite的REST接口

2024-05-14 07:50:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图让NetSuite Restlet使用urllib与Python 3.3一起工作,但是我似乎无法获得授权来获取并持续返回一个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帮助用于在位于here的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'}

仍然不知道为什么不起作用。。。。


Tags: comaddapplicationemailrequestaccountpasswordurllib
1条回答
网友
1楼 · 发布于 2024-05-14 07:50:10

问题与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.

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

相关问题 更多 >

    热门问题