从 Twitch 的 API 获取信息以检查 discord 机器人的主播是否在线
Getting info from Twitch's API to check if a streamer is online for a discord bot
我正在尝试制作一个 Discord 机器人,以在您键入“$live”时检查 Twitch 上的主播是否在线,但我无法让它工作。我正在使用 Discord.py。我的主要问题是使用 headers 调用 API 来提供我的授权。此外,使用我目前编写的代码,它会发送此错误。我的主要问题是,我如何提供授权密钥,这样它既不会给出下面的错误,也不会说需要 OAuth。 TIA!
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 19, in on_message
response = requests.get(url, headers=headers)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/sessions.py", line 528, in request
prep = self.prepare_request(req)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/sessions.py", line 456, in prepare_request
p.prepare(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 317, in prepare
self.prepare_headers(headers)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 449, in prepare_headers
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'
下面的代码。
import discord
import os
import requests
client = discord.Client()
url = 'https://api.twitch.tv/helix/search/channels?query=whyoscar'
headers = {'client_id:' '*******************', 'Authorization:' 'Bearer **********************'}
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$live'):
response = requests.get(url, headers=headers)
details = response.json()
await message.channel.send(details)
client.run(os.getenv('TOKEN'))
根据 Twitch 论坛上的 this post,您将需要一个用户令牌,而不是应用程序令牌。
Twitch API 文档说
OAuth or App Access Token required
...但没有指定令牌需要是用户令牌。
无论如何,转到 TwitchTokenGenerator,select Bot Chat Token,使用您的 Twitch 信息登录,它应该可以工作。
如果您使用 OAuth 访问 Twitch API,那么流程应该是这样的:
authURL = 'https://id.twitch.tv/oauth2/token'
client_ID = 'xxx'
secret = 'xxx'
AutParams = {'client_id': client_ID,
'client_secret': secret,
'grant_type': 'client_credentials'
}
AutCall = requests.post(url=authURL, params=AutParams)
access_token = AutCall.json()['access_token']
head = {
'Client-ID' : client_ID,
'Authorization' : "Bearer " + access_token
}
答案是,你想像这样定义你的 headers:url = 'https://api.twitch.tv/helix/search/channels?query=<channel you want to search for>&first=1' headers = {"client-id": os.getenv('CLIENT_ID_TOKEN'), "Authorization": os.getenv('ACCESS_TOKEN')}
然后把它们放在 response = requests.get(url, headers=headers)
中,然后得到 JSON 的响应并检查is_live=true
的回复。就这些!
我正在尝试制作一个 Discord 机器人,以在您键入“$live”时检查 Twitch 上的主播是否在线,但我无法让它工作。我正在使用 Discord.py。我的主要问题是使用 headers 调用 API 来提供我的授权。此外,使用我目前编写的代码,它会发送此错误。我的主要问题是,我如何提供授权密钥,这样它既不会给出下面的错误,也不会说需要 OAuth。 TIA!
Ignoring exception in on_message Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "main.py", line 19, in on_message response = requests.get(url, headers=headers) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/api.py", line 76, in get return request('get', url, params=params, **kwargs) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/sessions.py", line 528, in request prep = self.prepare_request(req) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/sessions.py", line 456, in prepare_request p.prepare( File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 317, in prepare self.prepare_headers(headers) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 449, in prepare_headers for header in headers.items(): AttributeError: 'set' object has no attribute 'items'
下面的代码。
import discord
import os
import requests
client = discord.Client()
url = 'https://api.twitch.tv/helix/search/channels?query=whyoscar'
headers = {'client_id:' '*******************', 'Authorization:' 'Bearer **********************'}
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$live'):
response = requests.get(url, headers=headers)
details = response.json()
await message.channel.send(details)
client.run(os.getenv('TOKEN'))
根据 Twitch 论坛上的 this post,您将需要一个用户令牌,而不是应用程序令牌。
Twitch API 文档说
OAuth or App Access Token required
...但没有指定令牌需要是用户令牌。
无论如何,转到 TwitchTokenGenerator,select Bot Chat Token,使用您的 Twitch 信息登录,它应该可以工作。
如果您使用 OAuth 访问 Twitch API,那么流程应该是这样的:
authURL = 'https://id.twitch.tv/oauth2/token'
client_ID = 'xxx'
secret = 'xxx'
AutParams = {'client_id': client_ID,
'client_secret': secret,
'grant_type': 'client_credentials'
}
AutCall = requests.post(url=authURL, params=AutParams)
access_token = AutCall.json()['access_token']
head = {
'Client-ID' : client_ID,
'Authorization' : "Bearer " + access_token
}
答案是,你想像这样定义你的 headers:url = 'https://api.twitch.tv/helix/search/channels?query=<channel you want to search for>&first=1' headers = {"client-id": os.getenv('CLIENT_ID_TOKEN'), "Authorization": os.getenv('ACCESS_TOKEN')}
然后把它们放在 response = requests.get(url, headers=headers)
中,然后得到 JSON 的响应并检查is_live=true
的回复。就这些!