Search code examples
djangotwiliodjango-two-factor-auth

TwilloRestfullException on inbound call in django_two_factor_auth


I am using django_two_factor_auth version 1.13.1 with python 3.6, and when I try to get OTP via phone call it returns TwilloRestfullExeption on URL.

HTTP 400 error: Unable to create a record: Url is not a valid URL: http://localhost:8000/twilio/inbound/two_factor/206039/?locale=en

in my URLS.py I have added the URLs paths

url('', include(tf_urls)),
url('', include(tf_twilio_urls)),

one more thing when I try to hit that URL via postman or web browser I got 200 success response

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Say language="en">Your token is 1. 7. 3. 8. 4. 7. Repeat: 1. 7. 3. 8. 4. 7. Good bye.</Say>
</Response>

Solution

  • The issue is that the application is running on localhost. When django_two_factor_auth makes the request to make a phone call, it passes a URL so that when the call connects, Twilio makes a request to that URL to find out what to do next. When running on localhost, that URL is built up as a localhost URL (as you can see in the error message), but Twilio is unable to make requests from the public internet to your localhost.

    I recommend using a tool like ngrok to make your localhost available to the internet and then trigger the 2 factor flow by visiting your django site through the ngrok URL. You can read more about how to install and use ngrok with django in this blog post.