【问题标题】:Error with new Twitch Api Python Oauth token is missing缺少新的 Twitch Api Python Oauth 令牌的错误
【发布时间】:2020-08-16 09:17:08
【问题描述】:
import requests

if __name__ == '__main__':
    API = 'https://api.twitch.tv/helix/streams?user_login=rediban'
    Client_ID = "myid"
    OAuth = "mytoken"

    head = {
        'Client-ID': Client_ID,
        'OAuth': OAuth,
    }
    rq = requests.get(url=API, headers=head)
    print(rq.text)

我想在流中显示当前的实时查看器。 当我启动脚本时,它显示 {"error":"Unauthorized","status":401,"message":"OAuth token is missing"}。 希望你能帮助我:)

【问题讨论】:

  • 您对Client-IDOAuth 标头的使用与dev.twitch.tv/docs/… 中显示的不匹配。
  • 如何生成 oauth 密钥
  • 获取令牌也显示在该页面上。
  • 我试过了,但我不明白。我被困在这个日子里,旧的 api 好多了
  • 如果您的代码有特定问题,请提供minimal reproducible example。否则,您必须联系他们的支持人员。

标签: python python-3.x api twitch twitch-api


【解决方案1】:

这是一个示例,您需要提供自己的 ClientID 和 Secret 以及要查找的 stream_name。您可以在https://dev.twitch.tv/console/apps注册CID

此示例将每次生成一个客户端凭据令牌,这是不好的做法。您应该(通常)生成并存储然后重用令牌直到它过期。

它生成一个令牌,然后调用流 API 来查找直播状态


import requests

client_id = ''
client_secret = ''
streamer_name = ''

body = {
    'client_id': client_id,
    'client_secret': client_secret,
    "grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)

#data output
keys = r.json();

print(keys)

headers = {
    'Client-ID': client_id,
    'Authorization': 'Bearer ' + keys['access_token']
}

print(headers)

stream = requests.get('https://api.twitch.tv/helix/streams?user_login=' + streamer_name, headers=headers)

stream_data = stream.json();

print(stream_data);

if len(stream_data['data']) == 1:
    print(streamer_name + ' is live: ' + stream_data['data'][0]['title'] + ' playing ' + stream_data['data'][0]['game_name']);
else:
    print(streamer_name + ' is not live');

【讨论】:

  • 嗨,我如何从这里向我的频道发送消息?我发现的唯一教程使用 kraken! (我也希望它能够在我不流式传输时工作,以防有人想随时使用该命令)
  • 然后您需要创建一个连接到 IRC 服务器的聊天机器人。不涉及海妖(或螺旋)。
猜你喜欢
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
相关资源
最近更新 更多