I am not sure what my authentication issue is but my oAuth token seems to always be wrong

the error I am getting:

  • Connected to irc-ws.chat.twitch.tv:443
    [11:32] error: No response from Twitch.
    Access Token: 21XXXXXXXXXXXXXXXXXXXXXXXX
    Error creating poll: Error: HTTP error! status: 401
    at startPoll (file:///XXXXXXXXXXXXXXXXXXXXXXX/bot.js:123:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

my code i am using where the error come from in my js:

I am Using TMI and https://twitchtokengenerator.com/

I am fully new to the twitch bot scene so this might be glaringly obvious… sorry

First off, it’s not recommended to use 3rd party token generators, they are not run by Twitch and so there are risks involved. You should ideally generate tokens yourself.

As for the issue, it could be a number of things. You don’t show what fetchAccessToken() does, so we have no way to know about that. It could be the token you have doesn’t have the channel:manage:polls scope, it could be that the token is not for the correct user (eg, if you created a token with your own account, you can only create a poll on your own account, if you want to create a poll on another channel then that other channel will need to go through the OAuth flow and grant you a token).

Also it would be helpful if you log the body of the response, as that will contain more information about the error rather than just logging the status code.

1 Like

my bad sorry… I have not been able to be smart enough to manage the scope of the oauth token like manging polls and chats thats why i used the third party token… but good point …
this is my fetchAccessToken function:
// Function to fetch a new access token
async function fetchAccessToken() {
try {
const response = await fetch(https://id.twitch.tv/oauth2/token, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’
},
body: new URLSearchParams({
client_id: clientId,
client_secret: clientSecret,
grant_type: ‘client_credentials’
})
});

    if (!response.ok) {
        throw new Error(`Failed to fetch access token: ${response.statusText}`);
    } //else {
        //log response
        //console.log(response);}

    const data = await response.json();
    accessToken = data.access_token;
    console.log('Access Token:', accessToken);
} catch (error) {
    console.error('Error fetching access token:', error);
    process.exit(1);
}

}

I created a separate twitch account which would act as the bot an created the app here in the twitch dev console. I then used the bot account identity to log on to my channel using this:

const opts = {
identity: {
username: ‘SlurbScatBot’, // Your bot’s username
password: ‘oauth:uz2XXXXXXXXXXXXXXXXXXX’ // OAuth token for authentication this is from tmi
},
channels: [
‘slurbisaur’ // The channel the bot will join
]
};

For API cred s I used the info on the twitch dev console i got after creating the app

my response looks like this:

grant_type: ‘client_credentials’

Client Credentials creates an App token, not a User token. As such as it has no user permissions as it doesn’t represent a user, and so for the most part it can’t use any endpoints that requires scopes from a user.

i assume I would then have to use a redirect URi which one exactly is that ?

If this is just for your own use, I’d recommend just using the Twitch CLI. It can help you generate User Tokens, will temporarily host a server to handle the OAuth process, and you’ll have an Access Token and a Refresh Token so that you can programmatically get new tokens as the old ones expire and not have to go through the OAuth process again.

So I should avoid a separate bot account entirely ?

To answer your question:

Under user oAuth:

  • The user comes to your webpage (yes that could be you)
  • Then get sent to twitch to authorized the ClientID access
  • They come back to your webpage with a ?code= (if the user accepts the link request)
  • You exchange the code for a token (and refresh token)

So the redirect URI is the URL to the script that handles the code to token exchange. So only you know what it is once you have built it and put it somewhere.

That could be a page on http://localhost if it’s just for personal use or a publically accessable website for ease of key generation (without having to start the local server first).

The TwitchCLI as dist mentioned does this all for you inside itself with a redirect URI set by the people that wrote the TwitchCLI (http://localhost:3000 iirc check ths documentation for sure and setup instructions)

You need the bot account and auth from it to read/send messages to chat as the bot.

You need auth from the channel that you want to run polls on.

So you need the bot account if you want to send messages in chat as the bot

1 Like

ok so I setup Twitch CLI and gave it my app creds it generated an user access token for me with: " twitch token -u -s chat:edit chat:read channel:manage:polls channel:read:polls"
this token i then used in my script instead of the access token and removed my fetchAccessToken() function.
I sadly am still getting no response from twitch
but the bot seems to be able to join the chat: “” * Connected to irc-ws.chat.twitch.tv:443""

BTW, do i have to create the application the dev console in my actual channel account or can I do that form the bot account ?

The owner of the ClientID is irrelevant

Personally I would own it under my main account, saves having to logout/login to do management of the ClientID

Well you want do do

as your broadcaster account
twitch token -u -s "channel:manage:polls channel:read:polls"

as your bot account
twitch token -u -s "chat:edit chat:read"

Two different twitch token calls one for each user that you need the token of

ahh ok I see, so it is not possible to have the bot manage the poll and the chat results as well as posting the pol to the chat? because currently I have the application created on my bot account with clientID and secret… the main account is only referenced in my code as the channel the bot would join… do I use the oauth: “usertoken” and then below in api creds for the access token the same one?

You can but the script uses the broadcasters token.
So it creates/manages the poll using another key

So you use token for the bot to connect to chat
And you use the token for the broadcaster to make the API calls to create/manage a poll

Mod note: removed your image as it leaked your client secret. (with should be kept secret)
ClientID’s are considered public, which you did censor…
Tokens are natuarlly secret, which you did censor)

As per the image, that I removed, a username is alllowercase not WithCapitalLetts

And a channel name is JUST the username slurbisaur not the URL to the channel, offhand I forget if tmi.js requires this to be prefixed with a # or not, but I don’t use tmi.js

So channels might in fact need to be

channels: [ '#slurbisaur' ]

sorry my bad for that it was a deprecated secret i left in on accident

for clarification, for me in order to generate an access token for the bot in the twitch cli i would use the app created on the bot’s account dev console ? in order to even get a separate token for my broadcaster account i would need a different app on that account and client ID to get a new token form Twitch cli or can I just generate another one ?

No the owner of the Application is irrelevant

No

When you do twitch token -u opens the URL in your browser automatically

You open that URL and then check it’s the account you want to link.

So what I tell people when they need to auth their and their bot account to me is

  • open this link in your main browser and grant your broadcaster account
  • open this other link in an incognito session in your browser, login to twitch as your bot and then grant access

So the ClientID is owned by me and then I have two different tokens for two different users with different permissions

So in summary do the following

  • Click on you browser in a normal window
  • Run twitch token -u -s "channel:manage:polls channel:read:polls"
  • Grant access if prompted with your broadcaster account
  • Record the resultant access and refresh token

Then

  • Open a incognito browser window
  • Run twitch token -u -s "chat:edit chat:read"
  • This’ll open the auth dialog in the incognito browser window, then login with your bot account
  • Record the resultant access and refresh token

okay i do have the refresh tokens for both scopes as well as the access tokens saved away now … they will expire in 2 hours

btw sorry for being so dimlit on this here… i always struggled with auth and access issues

well it seems that did at least somewhat work since I am not getting 401 for auth anymore but am getting 403 now

Whats the body response say?

HTTP code is half the information

Yeah for a chatbot to read/write the token only needs to be valid at connect

So when you start/wnat to connect use the refresh to get a new access token and then connect

at startPoll (file:///xxxxxxxxxxxxxxxxxxxxxxxxxbot.js:95:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Thats the error that your code spat out.

Since your call checks response.ok you need to logout the response body

Broadly, isntead or in addition to your throw

let response = await fetch(yaddatdadda);

if (!response.status != 200) {
    console.log('Failed with', response.status, 'body:', await response.text());
    return;
}
1 Like