【发布时间】:2021-01-15 11:33:01
【问题描述】:
我正在使用 Azure Function Apps 从 Xero 接收 webhook 进行处理。但是,我的 HASH 计算不正确。
我认为这与 Python 处理正文请求的方式有关。
代码如下:
#import Logging module to log errors to console
import logging
#import Azure Functions
import azure.functions as func
#import Cryptopackages
import hmac
import hashlib
import base64
#import JSON
import json
XERO_KEY = 'key here'
#function to generate key
def create_sha256_signature(key, message):
messagecoded = bytes(message, 'UTF-8')
return base64.b64encode(hmac.new(key.encode(), messagecoded, digestmod=hashlib.sha256).digest()).decode()
#Define Main function - Recieve a HTTP reponse
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Recieved a Request from Xero')
#Parse HASH Key from Header
xerosign = req.headers.get('x-xero-signature')
logging.info(xerosign)
#Get JSON body
Data = req.get_body()
Body = Data
#Body = json.dumps(payload, separators=(",", ":")).strip()
logging.info(Body)
#Get Signature from defined function
signature = create_sha256_signature(XERO_KEY, str(Body))
logging.info(signature)
logging.info(xerosign)
if hmac.compare_digest(xerosign, signature):
return func.HttpResponse(
status_code=200)
else:
return func.HttpResponse(
status_code=401
)
正文请求是
{
"events": [],
"lastEventSequence": 0,
"firstEventSequence": 0,
"entropy": "TXHKKDMKUHWFBYTVJSQU"
}
标头 x-xero-signature = C7G5M2SVxs/fAJM8fFLahzex32Rr7jwMHixX/OgvkFQ=
【问题讨论】:
-
欢迎来到 SO。请更清楚地描述您的问题。
-
问题是,我在运行上述脚本时收到了不同的计算哈希值。在代码中的某个地方,主体正在被修改或转换。这导致哈希发生变化。
标签: python azure-function-app xero-api