Intento verificar la validez de un token de chat de contracción y devuelvo verdadero o falso dependiendo de eso. Si solicito un token incorrecto, funciona, pero tan pronto como solicito un token válido, no obtengo ningún valor. Simplemente no puedo entender lo que estoy haciendo mal
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 } })Para validar un token, debe usar Validate Endpoint en lugar de tmi.js
Entonces haces la siguiente llamada
curl -X OBTENER 'https://id.twitch.tv/oauth2/validate'
-H 'Autorización: Portador'
No anteponga su token con oauth:
O para una llamada javascript/fetch básica:
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'; });Consulte la documentación: https://dev.twitch.tv/docs/authentication#validating-requests