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