Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventSub Not Registering subscriptions with Twitch API #306

Open
the-ivo-robotnic opened this issue May 9, 2024 · 1 comment
Open

EventSub Not Registering subscriptions with Twitch API #306

the-ivo-robotnic opened this issue May 9, 2024 · 1 comment

Comments

@the-ivo-robotnic
Copy link

the-ivo-robotnic commented May 9, 2024

Problem Statement:

It appears as though pyTwithchAPI is not actually registering subscriptions with Twitch.

Description:

I'm trying to setup the most basic event-handler service which is almost a carbon copy of the example code from the ReadTheDocs:

#!/usr/bin/env python
import os
import asyncio
import logging
import json
from twitchAPI.helper import first
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import AuthScope, UserAuthenticator
from twitchAPI.eventsub.webhook import EventSubWebhook
from twitchAPI.eventsub.websocket import EventSubWebsocket
from twitchAPI.object.eventsub import ChannelSubscriptionGiftEvent

APP_ID = os.getenv("OMT_CLIENT_ID")
APP_SECRET = os.getenv("OMT_CLIENT_SECRET")
TARGET_USERNAME = "the_ivo_robotnic"
CALLBACK_URL = "https://[redacted]/"
USER_TOKEN_PATH = f'{os.getcwd()}/user_token.json'
CALLBACK_PORT = 7071
TARGET_SCOPES = [AuthScope.USER_READ_SUBSCRIPTIONS]
CALLBACK_TIMEOUT = 3  # Seconds

# Configure logger
logging.basicConfig(level=logging.DEBUG)
LOG = logging.getLogger(__name__)


async def on_subscribe(data: ChannelSubscriptionGiftEvent) -> None:
    e = data.event  # Truncate the event object
    LOG.info(f"{e.user_name} gifted {e.total} {e.total}x {e.tier} subs!")


async def main():
    # Create and authenticate Twitch API Client
    twitch = await Twitch(APP_ID, APP_SECRET)
    user = await first(twitch.get_users(logins=TARGET_USERNAME))
    auth = UserAuthenticator(twitch, TARGET_SCOPES)
    access_token = await auth.authenticate()
    with open(USER_TOKEN_PATH, "w+") as file:
        json.dump(access_token, file)
        LOG.debug(f'Wrote cached user token to -> {USER_TOKEN_PATH}')

    # Create the EventSub Manger
    eventsub = EventSubWebhook(CALLBACK_URL, CALLBACK_PORT, twitch=twitch)
    eventsub.wait_for_subscription_confirm = True
    eventsub.wait_for_subscription_confirm_timeout = CALLBACK_TIMEOUT

    # Start the EventSub Client
    eventsub.start()

    # Unscrubscribe from everything to clear artifacts
    await eventsub.unsubscribe_all()

    # Listen for Events
    LOG.debug("Beginning subscriptions... ")
    await eventsub.listen_channel_subscription_gift(user.id, on_subscribe)
    LOG.debug("Done!")

    try:
        # Block on user input for shutdown
        input("Press any key to shut down...")
    finally:
        await eventsub.stop()
        await twitch.close()


asyncio.run(main())

STDOUT is:

DEBUG:asyncio:Using selector: EpollSelector
DEBUG:twitchAPI.twitch:generating fresh app token
DEBUG:twitchAPI.twitch:making GET request to https://api.twitch.tv/helix/users?login=the_ivo_robotnic
DEBUG:asyncio:Using selector: EpollSelector
INFO:twitchAPI.oauth:running oauth Webserver
DEBUG:twitchAPI.oauth:got callback with state 20944040-7b07-48f5-9a91-b7d2caf7a759
INFO:aiohttp.access:127.0.0.1 [08/May/2024:19:22:21 -0700] "GET /?code=9csyzkrrs5m2yclnqt2388ci9h59ms&scope=user%3Aread%3Asubscriptions&state=20944040-7b07-48f5-9a91-b7d2caf7a759 HTTP/1.1" 200 452 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0"
INFO:aiohttp.access:127.0.0.1 [08/May/2024:19:22:22 -0700] "GET /favicon.ico HTTP/1.1" 404 173 "http://localhost:17563/?code=9csyzkrrs5m2yclnqt2388ci9h59ms&scope=user%3Aread%3Asubscriptions&state=20944040-7b07-48f5-9a91-b7d2caf7a759" "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0"
INFO:twitchAPI.oauth:shutting down oauth Webserver
DEBUG:__main__:Wrote cached user token to -> /home/ivo/repos/shift/obs-media-triggers/src/user_token.json
DEBUG:asyncio:Using selector: EpollSelector
INFO:twitchAPI.eventsub.webhook:started twitch API event sub on port 7071
DEBUG:twitchAPI.twitch:making GET request to https://api.twitch.tv/helix/eventsub/subscriptions
DEBUG:__main__:Beginning subscriptions... 
DEBUG:twitchAPI.eventsub.webhook:subscribe to channel.subscription.gift version 1 with condition {'broadcaster_user_id': '128300706'}
DEBUG:twitchAPI.eventsub.webhook:subscription for channel.subscription.gift version 1 with condition {'broadcaster_user_id': '128300706'} has id 7439eda8-095f-403c-b5d8-ff952aa83b15
DEBUG:twitchAPI.eventsub.webhook:received challenge for subscription 7439eda8-095f-403c-b5d8-ff952aa83b15
INFO:aiohttp.access:10.8.0.1 [08/May/2024:19:22:26 -0700] "POST /callback HTTP/1.1" 200 195 "-" "Go-http-client/1.1"
DEBUG:__main__:Done!
Press any key to shut down...

So everything appears to be working... On the surface, but When running:

clear && twitch event -F http://localhost:7071/callback trigger channel.subscribe -f dingus

It results in:

ERROR:twitchAPI.eventsub.webhook:received event for unknown subscription with ID 120e3697-734a-d890-1dcb-14377f3cf221
INFO:aiohttp.access:127.0.0.1 [08/May/2024:19:23:12 -0700] "POST /callback HTTP/1.1" 200 150 "-" "twitch-cli/1.1.22"

And when separately checking with cURL my active subscriptions- while the service is still running:

clear && curl -X GET -H "Client-Id: $OMT_CLIENT_ID" -H "Authorization: Bearer $AT" https://api.twitch.tv/helix/eventsub/subscriptions

It responds with essentially no active subscriptions:

{"total":0,"data":[],"max_total_cost":10,"total_cost":0,"pagination":{}}

So it seems as though the subscriptions are not being fully registered, and there's nothing jumping out at me as to why.

Any ideas as to what I might be missing and/or what's going wrong?

I appreciate the help,
Ivo R.

@Teekeks
Copy link
Owner

Teekeks commented May 9, 2024

As described here: https://pytwitchapi.dev/en/latest/tutorial/mocking.html#mocking-eventsub-webhook
You must also specify at minimum -u and -s when mocking calls with the CLI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants