Skip to content

Instantly share code, notes, and snippets.

@RobSpectre
Created October 18, 2013 11:03
Show Gist options
  • Save RobSpectre/7039981 to your computer and use it in GitHub Desktop.
Save RobSpectre/7039981 to your computer and use it in GitHub Desktop.
Validating requests on GAE
import webapp2
import logging
from twilio.util import RequestValidator
from twilio import twiml
AUTH_TOKEN = 'xxxxx'
class MainPage(webapp2.RequestHandler):
def twilio_request_validator(self):
return RequestValidator(AUTH_TOKEN)
def post(self):
response = twiml.Response()
validator = self.twilio_request_validator()
signature = validator.validate(self.request.url, self.request.POST,
self.request.headers['X-Twilio-Signature'])
logging.info("Validation: %s" % str(signature))
logging.info("Computed signature: %s" %
str(validator.compute_signature(self.request.url,
self.request.POST)))
logging.info("Received signature: %s" %
self.request.headers['X-Twilio-Signature'])
logging.info("Request URL: %s" % self.request.url)
logging.info("POST Params: %s" % self.request.POST)
s = self.request.url
for k, v in sorted(self.request.POST.items()):
s += k + v
logging.info("Generated string: %s" % s)
if signature:
response.message("Works!")
else:
response.message("SHIT.")
self.response.headers['Content-Type'] = 'text/xml'
self.response.write(str(response))
application = webapp2.WSGIApplication([
('/sms', MainPage)
], debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment