I try to check a twitch chat token for its validity and return true or false depending on that. If I ask for an incorrect token, it works, but as soon as I ask for a valid token, I get no value back. I just can't figure out what I'm doing wrong
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
}
})
To validate a token, you should use the Validate Endpoint instead of tmi.js
So you do the following call
curl -X GET 'https://id.twitch.tv/oauth2/validate'
-H 'Authorization: Bearer '
Don't prefix your token with oauth:
Or for a basicish javascript/fetch call:
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';
});
See documentation: https://dev.twitch.tv/docs/authentication#validating-requests
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.