Created
May 4, 2020 06:34
-
-
Save and2long/85e38ea6f668e4b9e758dfbf5c966e5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Amazon Gift Card | |
官网网页测试地址: | |
https://s3.amazonaws.com/AGCOD/htmlSDKv2/htmlSDKv2_NAEUFE/index.html | |
可对参数进行逐一对照。 | |
官网API文档: | |
Amazon Gift Card Incentives API:https://developer.amazon.com/zh/docs/incentives-api/incentives-api.html | |
""" | |
import datetime | |
import hashlib | |
import hmac | |
import sys | |
import requests | |
method = 'POST' | |
service = 'AGCODService' | |
host = 'agcod-v2-fe-gamma.amazon.com' | |
region = 'us-west-2' | |
endpoint = 'https://agcod-v2-fe-gamma.amazon.com/CreateGiftCard' | |
content_type = 'application/json' | |
amz_target = 'com.amazonaws.agcod.AGCODService.CreateGiftCard' | |
parameters = """{"creationRequestId":"You77666","partnerId":"You77","value":{"currencyCode":"JPY","amount":1000}}""" | |
# TODO | |
access_key = '' | |
secret_key = '' | |
def sign(key, msg): | |
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest() | |
def get_signature_key(key, date_stamp, region_name, service_name): | |
kDate = sign(('AWS4' + key).encode('utf-8'), date_stamp) | |
kRegion = sign(kDate, region_name) | |
kService = sign(kRegion, service_name) | |
kSigning = sign(kService, 'aws4_request') | |
return kSigning | |
if access_key is None or secret_key is None: | |
print('No access key is available.') | |
sys.exit() | |
t = datetime.datetime.utcnow() | |
amz_date = t.strftime('%Y%m%dT%H%M%SZ') | |
date_stamp = t.strftime('%Y%m%d') | |
canonical_uri = '/CreateGiftCard' | |
canonical_querystring = '' | |
# Note that there is a trailing \n. | |
canonical_headers = 'accept:{}\nhost:{}\nx-amz-date:{}\nx-amz-target:{}\n'.format(content_type, | |
host, | |
amz_date, | |
amz_target) | |
signed_headers = 'accept;host;x-amz-date;x-amz-target' | |
payload_hash = hashlib.sha256(parameters.encode('utf-8')).hexdigest() | |
print("\nHashed Payload") | |
print(payload_hash) | |
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash | |
print("\nCanonical Request:") | |
print(canonical_request) | |
algorithm = 'AWS4-HMAC-SHA256' | |
credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request' | |
string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashlib.sha256( | |
canonical_request.encode('utf-8')).hexdigest() | |
print("\nString To Sign:") | |
print(string_to_sign) | |
signing_key = get_signature_key(secret_key, date_stamp, region, service) | |
signature = hmac.new(signing_key, string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest() | |
print("\nsignature:") | |
print(signature) | |
authorization_header = "{} Credential={}/{},SignedHeaders={},Signature={}".format(algorithm, | |
access_key, | |
credential_scope, | |
signed_headers, | |
signature) | |
print("\nAuthorization:") | |
print(authorization_header) | |
headers = {'Content-Type': content_type, | |
'accept': content_type, | |
'X-Amz-Date': amz_date, | |
'X-Amz-Target': amz_target, | |
'Authorization': authorization_header} | |
print("\nHeaders:") | |
print(headers) | |
# ************* SEND THE REQUEST ************* | |
print('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++') | |
print('Request URL = ' + endpoint) | |
r = requests.post(endpoint, data=parameters, headers=headers) | |
print('\nRESPONSE++++++++++++++++++++++++++++++++++++') | |
print('Response code: %d\n' % r.status_code) | |
print(r.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment