I know this is similar to other posts, but I read them all and none of the other posts have a solution that works for me. I develop REST APIs for a living, so I am not new to this at all, but I am totally stumped. I am trying to make webhook subscriptions with a Django webserver.
I can request the subscription just fine. Twitch sends back a 202, and my server gets the challenge request. I send a Response with the challenge string and a 200 status, but then I never get any topics. Also when I poll the https://api.twitch.tv/helix/webhooks/subscriptions
endpoint it always shows 0 subscriptions.
I have checked my nginx logs and never get a POST request from Twitch to any subscription endpoint I requested. I have tried using and not using trailing slashes on the callback url, but have the exact same results. Does anyone know what I am doing wrong?
Here is my callback handler code:
@action(methods=['get', 'post'], detail=True, permission_classes=[AllowAny])
def twitchstreamstatus(self, request, pk=None):
"""Change the status of a stream.
Also handle Twitch stream webhook challenges.
"""
logger.debug('Subscription webhook call for stream %s', pk)
try:
stream = TwitchStream.objects.get(pk=pk)
except TwitchStream.NotFound:
logger.warning('Stream %s called by Twitch webhook but not found',
pk)
return Response()
# This is a subscription verification challenge
if request.method == 'GET':
logger.info(f'Challenge request for stream {pk}: {stream.display_name}')
token = request.query_params.get('hub.challenge').rstrip()
logger.debug('Returning token response: %s', token)
return Response(token)
data = request.data.copy()['data']
if not data or data == []:
logger.debug('Stream offline callback for stream %s', stream.name)
stream.go_offline()
return Response()
logger.debug('Stream online callback for stream %s', stream.name)
stream.go_online(data[0]['game_id'])
return Response()
and this is my subscription request code, which works since I am getting the challenge request just fine:
headers = {
'Client-ID': settings.TWITCH_CLIENT_ID,
'Content-Type': 'application/json'
}
sub_url = 'https://api.twitch.tv/helix/webhooks/hub'
payload = {
'hub.callback': f'{settings.SS_API_URL}/streams/{stream.id}/twitchstreamstatus/',
'hub.mode': 'subscribe',
'hub.topic': f'https://api.twitch.tv/helix/streams?user_id={stream.twitch_id}',
'hub.lease_seconds': int(os.environ.get('TWITCH_WEBHOOK_LEASE_SECONDS', 864000)),
'hub.secret': settings.TWITCH_WEBHOOK_SECRET
}
logger.debug('Subscription payload: %s', payload)
r = requests.post(sub_url, json=payload, headers=headers)
Any help is very much appreciated.