Last active
January 21, 2019 09:40
-
-
Save NgocLinhNguyen/3f184f016f174b797a637bd83ed0744d to your computer and use it in GitHub Desktop.
Create signature for API
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
from hashlib import md5 | |
import hmac | |
from collections import OrderedDict | |
def get_sign(token, type): | |
appSecret = '828eb89cf2f7b7517e6ec37bb34b34ee' | |
token_string = '' | |
token = OrderedDict(sorted(token.items())) | |
for key in token: | |
token_string += key + str(token[key]) | |
if type == 'md5': | |
token_string = appSecret + token_string + appSecret | |
token_string = token_string.encode('utf-8') | |
return md5(token_string).hexdigest().upper() | |
elif type == 'hmac': | |
token_string = token_string.encode('utf-8') | |
appSecret = appSecret..encode('utf8') | |
return hmac.new(appSecret, token_string).hexdigest().upper() | |
return None | |
if __name__ == '__main__': | |
token = { | |
"access_token": "50002300602qMj1120f2cfsLew9Kks1tPi8aF1DJxAwwFfEbsgPyTchERHAMZ0ZIfyt", | |
"refresh_token": "50003301802wPq170fc260sasuf9CfvAK0TEFluI3Ev0mlZ9rRFQOvrMuWAnXjgGOjn", | |
"w1_valid": 1550391762716, | |
"refresh_token_valid_time": 1547799762716, | |
"w2_valid": 1547801562716, | |
"user_id": "133358604203", | |
"expire_time": 1550391762716, | |
"r2_valid": 1548058962716, | |
"locale": "zh_CN", | |
"r1_valid": 1550391762716, | |
"sp": "ae", | |
"user_nick": "vn1242151528cooa" | |
} | |
print("md5: {}".format(get_sign(token, "md5"))) | |
print("hmac: {}".format(get_sign(token, "hmac"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment