使用OAuth查找Netsuite API的示例Python代码?

Jus*_* H. 4 python oauth netsuite

Netsuite的文档不会出现.有没有人有他们编写的代码可以帮助我生成有效的签名.

小智 10

NetSuite Suite答案网站中有一些示例代码,但您必须登录才能访问它.

https://netsuite.custhelp.com/app/answers/detail/a_id/42165/kw/42165

以下是我能够完成工作的答案代码.唯一的区别是他们的代码通过尝试将时间戳编码为int而破裂.我把它改为str,编码运行正常.密钥/令牌/领域来自他们的演示代码.插入自己的,你应该很高兴去.

import oauth2 as oauth
import requests
import time

url = "https://rest.netsuite.com/app/site/hosting/restlet.nl?script=992&deploy=1"
token = oauth.Token(key="080eefeb395df81902e18305540a97b5b3524b251772adf769f06e6f0d9dfde5", secret="451f28d17127a3dd427898c6b75546d30b5bd8c8d7e73e23028c497221196ae2")
consumer = oauth.Consumer(key="504ee7703e1871f22180441563ad9f01f3f18d67ecda580b0fae764ed7c4fd38", secret="b36d202caf62f889fbd8c306e633a5a1105c3767ba8fc15f2c8246c5f11e500c")

http_method = "GET"  
realm="ACCT123456"

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)
headery = header['Authorization'].encode('ascii', 'ignore')
headerx = {"Authorization": headery, "Content-Type":"application/json"}
print(headerx)
conn = requests.get("https://rest.netsuite.com/app/site/hosting/restlet.nl?script=992&deploy=1",headers=headerx)
print(conn.text)
Run Code Online (Sandbox Code Playgroud)

  • @LimyandiVicoTrico 你现在提出这个很有趣。我的修复刚刚被推送到产品中!您可以在这里找到我的解决方案 https://github.com/pietro-andreoli/python-oauth2-additions 。本质上,我扩展了 `SignatureMethod_HMAC_SHA1` 类的基类,以便为 SHA256 实现我自己的实现。不能保证是一个完美的解决方案,但现在我已经在努力了一点。如果有任何变化将会更新。 (2认同)