订阅流\u更改的webhook python twitch API始终失败

2024-04-27 03:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图订阅stream_changed webhook,并在用户使用python twitch API在twitch上上线时获得通知。 我把一切都设置好了,但是当我尝试订阅webhook时,我总是将订阅状态设置为false,这意味着我无法订阅webhook。 这是我正在使用的代码

from twitchAPI.twitch import Twitch
from twitchAPI.types import AuthScope
from twitchAPI.webhook import TwitchWebHook
from pprint import pprint
import json
import ssl

config = json.load(open('config.json', 'r'))
IP_ADDRESS = config.get('PUBLIC_ADDR')
PORT = config.get('PUBLIC_PORT')
APP_ID = config.get('APP_ID')
APP_SECRET = config.get('APP_SECRET')
PUBLIC_ADDR = f'https://{IP_ADDRESS}:{PORT}'
print(f'Public address : {PUBLIC_ADDR}')

TOKEN = config.get("TOKEN")
REFRESH_TOKEN = config.get("REFRESH_TOKEN")


def callback_stream_changed(uuid, data):
    print('Callback Stream changed for UUID ' + str(uuid))
    pprint(data)


# basic twitch API authentication, this will yield a app token but not a user token
auth_scope = [
    AuthScope.BITS_READ,
    AuthScope.USER_EDIT,
    AuthScope.WHISPERS_READ,
    AuthScope.CHANNEL_READ_SUBSCRIPTIONS,
    AuthScope.CHANNEL_READ_STREAM_KEY,
    AuthScope.ANALYTICS_READ_EXTENSION,
    AuthScope.ANALYTICS_READ_GAMES,
    AuthScope.CHANNEL_EDIT_COMMERCIAL,
    AuthScope.CHANNEL_READ_HYPE_TRAIN,
    AuthScope.CHANNEL_MANAGE_BROADCAST,
    AuthScope.CHANNEL_READ_REDEMPTIONS,
    AuthScope.CLIPS_EDIT,
    AuthScope.USER_EDIT_BROADCAST,
    AuthScope.USER_READ_BROADCAST,
    AuthScope.USER_READ_EMAIL,
    AuthScope.USER_EDIT_FOLLOWS,
    AuthScope.CHANNEL_MODERATE,
    AuthScope.CHAT_EDIT,
    AuthScope.CHAT_READ,
    AuthScope.WHISPERS_READ,
    AuthScope.WHISPERS_EDIT,
    AuthScope.MODERATION_READ,
    AuthScope.CHANNEL_SUBSCRIPTIONS
]
twitch = Twitch(APP_ID, APP_SECRET)
twitch.authenticate_app(auth_scope)
twitch.set_user_authentication(
    TOKEN, auth_scope, REFRESH_TOKEN)

sslContext = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)
hook = TwitchWebHook(PUBLIC_ADDR,
                     APP_ID, PORT, ssl_context=sslContext)
hook.authenticate(twitch)
hook.start()

user_info = twitch.get_users()
user_id = user_info['data'][0]['id']
print(f'User ID : {user_id}')


success, uuid_stream = hook.subscribe_stream_changed(
    user_id, callback_stream_changed)
print(f'Was subscription successfull?: {success}')

input('[+] Press enter to stop...')

success = hook.unsubscribe(uuid_stream)
print(f'was unsubscription successfull?: {success}')
# since hook.unsubscribe_on_stop is true, we dont need to unsubscribe manually, so lets just stop
hook.stop()

在这里,成功的价值是错误的。 如果我做错了什么或错过了什么,有人能指导我吗? 顺便说一句,这是我第一次使用TwitchAPI


Tags: importtokenconfigappreadstreamgetchannel