Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created June 5, 2017 20:57
Show Gist options
  • Save jcipriano/b2f9eff89129abee8df99b19a9d2310b to your computer and use it in GitHub Desktop.
Save jcipriano/b2f9eff89129abee8df99b19a9d2310b to your computer and use it in GitHub Desktop.
Python example for generating challenge response for Twitter webhooks
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