【问题标题】:How to produce Netsuite Suitetalk TBA authorization header in python?如何在 python 中生成 Netsuite Suitetalk TBA 授权标头?
【发布时间】:2020-11-05 15:00:41
【问题描述】:

我正在尝试使用 Python 编写的应用程序中的 suitetalk Web 服务 api。

没有太多文档,但从那里我尝试创建身份验证(HMAC-SHA256)但每次都得到这个: error="token_rejected", error_description="无效登录尝试。"

使用 netsuite 提供的集合在邮递员中使用相同的凭据,我能够登录并调用 API,因此我知道我的凭据很好,这不是权限问题。

邮递员调用和我的唯一区别是随机数、时间戳(这两个很明显)和签名。

我尝试使用带有以下代码的签名:

params = {
    'oauth_version': "1.0",
    'oauth_nonce': oauth.generate_nonce(),
    'oauth_timestamp': str(int(time.time())),
    'oauth_token': token.key,
    'oauth_consumer_key': consumer.key
}

req = oauth.Request(method=http_method, url=url, parameters=params)
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
header = req.to_header(realm)
return header['Authorization']

我认为这是 HMAC_SHA1 问题而不是 256,但该加密适用于邮递员。

我也试过: NetSuite python TBA Authentication

我很困惑,如果能提供一些有用的见解或代码 sn-p,我将不胜感激。

【问题讨论】:

标签: python suitetalk


【解决方案1】:

我是这样做的:

class NetsuiteAuth(object):

    def __init__(self):
            self.access_token = SANDBOX_ACCESS_TOKEN
            self.token_secret = SANDBOX_TOKEN_SECRET
            self.realm_uppercase = SANDBOX_REALM_uppercase
            self.deploy = SANDBOX_DEPLOY

    def get_headers(self, http_method, url):

        token = oauth.Token(key=self.access_token,
                            secret=self.token_secret)

        consumer = oauth.Consumer(key=CONSUMER_KEY,
                                  secret=CONSUMER_SECRET)

        # # crafting the request
        params = {
            'oauth_version': "1.0",
            'oauth_nonce': oauth.generate_nonce(),
            'oauth_timestamp': str(int(time.time())),
            'oauth_token': token.key,
            'oauth_consumer_key': consumer.key
        }
        signature_method = oauth.SignatureMethod_HMAC_SHA1()  # soon deprecated by Netsuite !

        req = oauth.Request(method=http_method, url=url, parameters=params)
        req.sign_request(signature_method, consumer, token)
        auth_header = req.to_header(self.realm_uppercase)
        auth_header_encoded = auth_header['Authorization'].encode('ascii', 'ignore')

        headers = {"Authorization": auth_header_encoded, "Content-Type": "application/json"}

        return headers

然后我像这样调用它并使用请求中的标头。

headers = NETSUITE_AUTH.get_headers('POST', URL)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多