【问题标题】:Unable to pass authentication information to NetSuite's REST interface using Python无法使用 Python 将身份验证信息传递给 NetSuite 的 REST 接口
【发布时间】:2013-09-25 04:42:17
【问题描述】:

我一直在尝试使用 urllib 让 NetSuite Restlet 与 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'}

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

【问题讨论】:

    标签: python netsuite


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      相关资源
      最近更新 更多