Last active
September 25, 2015 19:00
-
-
Save bonnie/9749497 to your computer and use it in GitHub Desktop.
Python file demonstrating signature calculation for 500friends Security API
This file contains hidden or 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
#python3 | |
from urllib.parse import urlencode,quote | |
from hashlib import md5 | |
from collections import OrderedDict | |
ACCOUNT_ID = 'ACCOUNT_ID' | |
SECRET_KEY = 'SECRET KEY' | |
def array_to_sorted_string(self, inArray): | |
# must stringify keys in order to make sure they get sorted alphabetically, not numerically | |
str_keys = {} | |
for key in inArray.keys(): | |
str_keys[str(key)] = inArray[key] | |
sorted_keys = list(str_keys.keys()) | |
sorted_keys.sort() | |
arg_string = '' | |
for key in sorted_keys: | |
val = str_keys[key] | |
if isinstance(val, dict): | |
arg_string += key + array_to_sorted_string(val) | |
else: | |
arg_string += key+ str(val) | |
return arg_string | |
arg_dict = {} | |
arg_dict['uuid'] = ACCOUNT_ID | |
arg_dict['type'] = 'EVENT_TYPE' | |
arg_dict['value'] = 'TOTAL_PURCHASE_VALUE' | |
arg_dict['products'] = { | |
'0': { | |
'product_id': 'SKU1', | |
'price': 'PRICE1', | |
'brands': 'BRANDS1' | |
}, | |
'1': { | |
'product_id': 'SKU2', | |
'price': 'PRICE2', | |
'brands': 'BRANDS2' | |
} | |
} | |
sig_string = SECRET_KEY + array_to_sorted_string(arg_dict) | |
arg_dict['sig'] = md5(sig_string.encode('utf-8')).hexdigest() | |
query_string = '' | |
for index, prod_dict in arg_dict['products'].items(): | |
for key, val in prod_dict.items(): | |
query_string += "products[" + index + "][" + key + "]=" + quote(val) + "&" | |
del arg_dict['products'] | |
query_string += urlencode(arg_dict) | |
final_url = "https://loyalty.500friends.com/data/points/show?" + query_string | |
print(final_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment