python HMAC签名已过期

时间:2019-11-22 06:17:56

标签: python

在Python上,HMAC签名已过期。但是,我不知道如何编码。起初,这很正常,但是很正常。正常输入了ACCESS_KEY,SECRET_KEY

错误消息

  

C:\ Python37 \ python.exe C:/Apache24/htdocs/coupang/01_call_url.py   {'code':'ERROR','message':'指定的签名已过期。',   'transactionId':'546a2e63-dd19-4877-9228-a57953242a07'}进程   以退出代码0结束

import hmac
import hashlib
import binascii
import os
import time
import requests
import json


REQUEST_METHOD = "POST"
DOMAIN = "https://api-gateway.coupang.com"
URL = "/v2/providers/affiliate_open_api/apis/openapi/v1/deeplink"

# Replace with your own ACCESS_KEY and SECRET_KEY
ACCESS_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

REQUEST = { "coupangUrls": [
    "https://www.coupang.com/np/search?component=&q=good&channel=user", 
    "https://www.coupang.com/np/coupangglobal"
]}


def generateHmac(method, url, secretKey, accessKey):
    path, *query = url.split("?")
    os.environ["TZ"] = "GMT+0"
    datetime = time.strftime('%y%m%d')+'T'+time.strftime('%H%M%S')+'Z'
    message = datetime + method + path + (query[0] if query else "")

    signature = hmac.new(bytes(secretKey, "utf-8"),
                         message.encode("utf-8"),
                         hashlib.sha256).hexdigest()

    return "CEA algorithm=HmacSHA256, access-key={}, signed-date={}, signature={}".format(accessKey, datetime, signature)


authorization = generateHmac(REQUEST_METHOD, URL, SECRET_KEY, ACCESS_KEY)
url = "{}{}".format(DOMAIN, URL)
resposne = requests.request(method=REQUEST_METHOD, url=url,
                            headers={
                                "Authorization": authorization,
                                "Content-Type": "application/json"
                            },
                            data=json.dumps(REQUEST)
                            )

print(resposne.json())

0 个答案:

没有答案