![](/img/trans.png)
[英]RxJS: Check token validity before calling S3 List, and wait for the token in case it is invalid
[英]check a twitch chat token for its validity
我嘗試檢查 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
}
})
要驗證令牌,您應該使用 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.