【问题标题】:check a twitch chat token for its validity检查抽搐聊天令牌的有效性
【发布时间】:2021-12-19 23:36:48
【问题描述】:

我尝试检查 twitch 聊天令牌的有效性并根据它返回 true 或 false。如果我要求一个不正确的令牌,它会起作用,但是一旦我要求一个有效的令牌,我就没有得到任何回报。我就是想不通我做错了什么

const tmi = require('tmi.js');
function checkToken(name, token, callback) {
  var errLogs = '';
  const client = new tmi.client({
    identity: {
      username: name,
      password: token
    },
    channels: ['channel']
  });
  client.connect()
    .catch(error => {
      console.log(error)
      if (error) {
        console.log('FAILED')
        errLogs = 'error';
        //return false;
      } else {
        console.log('VALID')
        errLogs = 'valid';
        //return true;
      }
      callback(errLogs);
    })
}
checkToken('username', 'oauth:XXXX...', function(res) {
  if (res) {
    console.log(res + ' TOKEN FAILED')
    ///... token is not Valid
  } else {
    console.log(res + ' TOKEN VALID')
    ///... token is Valid
  }
})

【问题讨论】:

    标签: javascript twitch


    【解决方案1】:

    要验证令牌,您应该使用 Validate Endpoint 而不是 tmi.js

    所以你执行以下调用

    curl -X GET 'https://id.twitch.tv/oauth2/validate'
    -H '授权:承载'

    不要在你的令牌前面加上oauth:

    或者对于基本的 javascript/fetch 调用:

                fetch(
                    'https://id.twitch.tv/oauth2/validate',
                    {
                        "headers": {
                            "Authorization": "Bearer " + access_token
                        }
                    }
                )
                .then(resp => resp.json())
                .then(resp => {
                    if (resp.status) {
                        if (resp.status == 401) {
                            //'This token is invalid: ' + resp.message;
                            return;
                        }
                        // 'Unexpected output with a status?';
                        return;
                    }
                    if (resp.client_id) {
                        client_id = resp.client_id;
                        // token is valid was was generated for that client_id
                        return;
                    }
                    // if got here unexpected output from twitch
                })
                .catch(err => {
                    console.log(err);
                    // 'An Error Occured loading token data';
                });
    

    查看文档:https://dev.twitch.tv/docs/authentication#validating-requests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 2018-12-30
      • 2020-10-21
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多