NodeJS - Trigger Uncaught Exception / Authentification Failed

Hey everyone,

I actually developed an analytics twitch bot.
I followed this documentation : Getting Started | Twitch Developers
And moreover, I checked how tmi.js recommends to start here : https://tmijs.com/
So, I tried first to connect to my chat room anonymously and it worked well :

[18:46] info: Connecting to irc-ws.chat.twitch.tv on port 443..
[18:46] info: Sending authentication to server..
[18:46] info: Connected to server.
* Connected to irc-ws.chat.twitch.tv:443 *
[18:46] info: Executing command: JOIN #<chatroom_name>
[18:46] info: Joined #<chatroom_name>

When I tried to connect it with my bot’s id, each time it gave me this error :

[19:02] info: Connecting to irc-ws.chat.twitch.tv on port 443..
[19:02] info: Sending authentication to server..
[19:02] error: Login authentication failed
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);  
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Login authentication failed".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I searched everywhere but I never find any solution for this specifically.
Hope you will found something !

Thanks in advance
daftmob

The the oAuth token is invalid and/or lacks the required scopes chat:read for example.

Yep, when I gave authorization and try to get my oauth code , I followed this doc : Getting OAuth Access Tokens | Twitch Developers
But, in the previous documentation post, Twitch tell us to remove this sentence : channel%3Amanage%3Apolls+channel%3Aread%3Apolls by this : chat%3Aread+chat&3Aedit
But, there is an error occured :

GET: https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=<your client id goes here>&redirect_uri=http://localhost:3300&scope=chat%3Aread+chat%3Aedit&state=c3ab8aa609ea11e793ae92361f002671


{"status":400,"message":"invalid scope requested: 'chat'"}

So, I tried to add it with the channel’s scope like this :

https://id.twitch.tv/oauth2/authorize?response_type=code
&client_id=<your client id goes here>
&redirect_uri=http://localhost:3300
&scope=channel%3Amanage%3Apolls+channel%3Aread%3Apolls+chat%3Aread+chat%3Aedit
&state=c3ab8aa609ea11e793ae92361f002671

If it not sending me an error, I’ll authentify myself then authorize my bot.
I can check, in my Twitch External Connections, if my bot is connected, and it always will

Due to the order of paramters the scope was appened to the redirect URI which likely resulted in weirdness. The Redirect URI should be URI encoded. I’m assuming you have replaced your real URI with the example from the docs. So I’m thinking your real URI includes a ? somewhere.

(Also Client ID’s are considered public so no real need to waste time omitting/editing your code)

Try chat:read+chat:edit

So for example: using my GitHub examples ClientID:

https://id.twitch.tv/oauth2/authorize?client_id=hozgh446gdilj5knsrsxxz8tahr3koz&redirect_uri=https%3A%2F%2Fbarrycarlyon.github.io%2Ftwitch_misc%2Fauthentication%2Fimplicit_auth%2F&response_type=token&scope=chat:read+chat:edit

The state shouln’t be copy pasted from the documentation it’s an example, a state should be unique for each call, as it’s part of the CSRF protection you should employ.

This shows you have authed with any (or no) scopes at all. So checking this page will not help with debugging this issue.

I didn’t see upper that scope was an optionnal argument.
I saw in your example, your response_type isn’t set on code like the documentation mentionned it.

https://id.twitch.tv/oauth2/authorize?response_type=token
&client_id=<your client id goes here>
&redirect_uri=http://localhost:3300
&scope=chat:read+chat:edit

And it works, I feel dumb, the correct type for response_type wasn’t mentionned but even in the main documentation, it write the code parameter’s value with the code that /authorize returned.

In anyway, I appreciate your help !

My example is to generate an implict auth code.
As we were debugging your scope issue. Not the token type.

a code will give you a code to exhcnage for an access token and refresh token.
a token will give you a non refreshable access token

Generally for a chat bot you would use the code flow not the token flow.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.