我正在尝试为 API 调用生成 oauth_signature。使用Postman,我生成了如下代码
import requests
import json
url = "https://xxxxxxxxxxxx"
payload = json.dumps({
"key1": "value",
"Key2": "value2"
})
headers = {
'Authorization': 'OAuth realm="xxxxxxxx",
oauth_consumer_key="xxxxxxxxxxxxxxx",
oauth_token="xxxxxxxxxxxx",
oauth_signature_method="HMAC-SHA256",
oauth_timestamp="1628552790",
oauth_nonce="xxxxxxxx",
oauth_version="1.0",
oauth_signature="xxxxxxxxxxxxxxx"',
'Content-Type': 'application/json',
'Cookie': 'NS_ROUTING_VERSION=LAGGING'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
我想使用 Python 生成 oauth_signature,因为 Postman 会自动生成它,而没有太多关于如何生成的文档
发出 Oauth 1.0 请求需要生成 oauth 签名,在我的例子中,我使用 Netsuite 的 SHA256,因此有一种基于时间戳和随机数的方法。 我希望这对你有用:
import time #To generate the OAuth timestamp
import urllib.parse #To URLencode the parameter string
import hmac #To implement HMAC algorithm
import hashlib #To generate SHA256 digest
from base64 import b64encode #To encode binary data into Base64
import binascii #To convert data into ASCII
import requests #To make HTTP requests
import random
## PARAMS ##
oauth_consumer_key = 'xxxx'
oauth_signature_method = 'HMAC-SHA256'
oauth_version = '1.0'
account = 0000
consumer_secret = "xxxx"
access_token = "xxxx"
token_secret = "xxxx"
## PARAMS ##
method = 'POST'
url = 'https://'+str(account)+'.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql'
body = "{\n\t\"q\": \"SELECT id, companyName, email, dateCreated FROM customer WHERE dateCreated >= '01/01/2022' AND dateCreated < '12/12/2022'\"\n}"
oauth_timestamp = str(int(time.time()))
oauth_nonce = ''.join(random.choices("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", k=11))
def create_parameter_string(oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version, token_id):
parameter_string = ''
#parameter_string = parameter_string + 'grant_type=' + grant_type
parameter_string = parameter_string + 'oauth_consumer_key=' + oauth_consumer_key
parameter_string = parameter_string + '&oauth_nonce=' + oauth_nonce
parameter_string = parameter_string + '&oauth_signature_method=' + oauth_signature_method
parameter_string = parameter_string + '&oauth_timestamp=' + oauth_timestamp
parameter_string = parameter_string + '&oauth_token=' + token_id
parameter_string = parameter_string + '&oauth_version=' + oauth_version
return parameter_string
parameter_string = create_parameter_string(oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version,access_token)
encoded_parameter_string = urllib.parse.quote(parameter_string, safe='')
encoded_base_string = method + '&' + urllib.parse.quote(url, safe='')
encoded_base_string = encoded_base_string + '&' + encoded_parameter_string
signing_key = consumer_secret + '&' + token_secret
def create_signature(secret_key, signature_base_string):
encoded_string = signature_base_string.encode()
encoded_key = secret_key.encode()
temp = hmac.new(encoded_key, encoded_string, hashlib.sha256).hexdigest()
byte_array = b64encode(binascii.unhexlify(temp))
return byte_array.decode()
oauth_signature = create_signature(signing_key, encoded_base_string)
encoded_oauth_signature = urllib.parse.quote(oauth_signature, safe='')
headers = {
'Content-Type': 'text/plain',
'prefer':'transient',
'Authorization': 'OAuth realm="{0}",oauth_consumer_key="{1}",oauth_token="{2}",oauth_signature_method="{3}",oauth_timestamp="{4}",oauth_nonce="{5}",oauth_version="{6}",oauth_signature="{7}"'.format(
str(account),oauth_consumer_key,access_token,oauth_signature_method, oauth_timestamp ,oauth_nonce,oauth_version ,encoded_oauth_signature)
}
print(headers)
response = requests.post(url, data=body, headers=headers)
print(response.text)
我正在使用上面的代码,但我再次遇到同样的错误
“{“类型”:“https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2”,“标题”:“未经授权”,“状态”:401,“o: errorDetails":[{"detail":"登录尝试无效。有关更多详细信息,请参阅 NetSuite UI 中的登录审核跟踪(位于设置 > 用户/角色 > 用户管理 > 查看登录审核跟踪)。","o:errorCode":"INVALID_LOGIN"}]}"