Created
June 5, 2017 20:57
-
-
Save jcipriano/b2f9eff89129abee8df99b19a9d2310b to your computer and use it in GitHub Desktop.
Python example for generating challenge response for Twitter webhooks
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
import base64 | |
import hmac | |
import hashlib | |
import json | |
# Example app consumer secret found in apps.twitter.com | |
APP_CONSUMER_SECRET = 'z3ZX4v7mAAUGykl3EcmkqbartmuW8VFOOzCloLx9Q45P0hLrFu' | |
# Example token provided by incoming GET request | |
TOKEN = '9b4507b3-9040-4669-9ca3-6b94edb50553' | |
def get_challenge_response(token): | |
''' Creates a HMAC SHA-256 hash created from the app TOKEN and your app Consumer Secret. | |
:param token: the token provided by the incoming GET request | |
:returns: string | |
''' | |
# create digest using token and app consumer secret | |
sha256_hash_digest = hmac.new(APP_CONSUMER_SECRET, msg=token, digestmod=hashlib.sha256).digest() | |
response = { | |
'response_token': 'sha256=' + base64.b64encode(sha256_hash_digest) | |
} | |
return json.dumps(response) | |
# prints result | |
print(get_challenge_response(TOKEN)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment