Search code examples
pythondjangowebhooksinstagram-graph-apifacebook-webhooks

Respond to Instagram webhook in Django


I set the callback-url for Instagram webhook, and when Instagram Get my callback-url I should respond to the hub.challenge to verify. Instagram GET request to my endpoint URL:

GET https://callback_url/webhooks?
  hub.mode=subscribe&
  hub.challenge=1120110039&
  hub.verify_token=meatyhamhock

My code in vews.py:

def getInstagramWebhook(request):
    if request.method == "GET":
        mode         = request.GET.get("hub.mode")
        challenge    = request.GET.get("hub.challenge")
        verify_token = request.GET.get("hub.verify_token")
        return challenge

But I have this error:

The URL couldn't be validated. Response does not match challenge, expected value="1120110039", received="\u003C!DOCTYPE html>\n\u003Chtm..."

I tried JsonResponse, HttpResponse, and redirect but not work


Solution

  • I finally found the solution to the problem :)

    I added content_type='text/plain' in the Response:

    return HttpResponse(challenge, content_type='text/plain')
    

    And Facebook verified my callback URL