Pubsub Auth with accesstoken?

Hello,
I’m currently testing around with pubsubs. At the moment I have the problem that I have to send an authtoken within the request. The problem is that I only have the accesstoken, refreshtoken … So how do I work around that problem, do I have to request the authtoken oder how can I use pubsubs without the authtoken.
When I pass the accestoken as authtoken it responses with:
{“type”:“RESPONSE”,“error”:“ERR_BADAUTH”,“nonce”:""}

My code
socket.onopen = (event: any) => {
console.log(“Connection established”);

        let data = {
            "type": "LISTEN",
            data: {
                "topics": ["channel-bits-events-v1.44322889"],
                "auth_token": accessToken
            }
        }
        socket.send(JSON.stringify(data));
    }

Hopefully anyone can help me with that problem
Thank you
Shorty

Your code is correct. You should pass the accessToken as the auth_token

But for the bits topics, you need a token with

  • v1 - any scope
  • v2 - bits:read

As documented

This is indicative you are passing a token with no scopes at all on it.

So do I have to pass a scope within the request or when I redirect the user to authenticate my app?
At the moment I only ask for the ‘user:edit’ scope.

Sry but I didnt quite get what to do exactly now.

Is your access token still valid?

I can request followers and other things so I guess yes

Try getting a token with bits:read and the v2 topic

If your token only has user:edit I wonder if you have stumbled on a bug, as user:edit is a helix scope and your token only has helix scopes

Hey, it still doesnt seem to work.

            let data = {
            "type": "LISTEN",
            data: {
                "topics": ["channel-bits-events-v2.46024993"],
                "auth_token": accessToken
            }
        }

And the credentials object I safe looks like this

1. channel: "InL_Shorty"

2. type: "twitch"

3. access_token: "XXX"

4. refresh_token: "XXX"

5. scope: "bits:read,user:edit"
  1. token_type: “bearer”
let data = {
    type: 'listen',
    nonce: ' sdfkhjasdjkfhsdjkafasdf',
    data: {
        topics: ["channel-bits-events-v2.46024993"],
        auth_token: 'abcabcabc'
    }
}
ws.send(JSON.stringify(data));

The Auth Token should NOT be preceeded by oAuth/Bearer. It should be JUST the token.

At the moment I request the authcode
`https://id.twitch.tv/oauth2/authorize?client_id=b0o7fl44x6a3hom3omdsey4c6asq83&redirect_uri=http://localhost:4200/auth/twitch&response_type=

code

&scope=user:edit bits:read`

So I have to change this to the auth token and get the access token with the authtoken aswell, right?

Yup.

You need to do

Step 1) Redirect user to twitch on that URL
Step 2) Exchange the returned code for a access token aka auth token (they are one and the same)
Step 3) ???
Step 4) Profit

Im really sorry for asking again but I got the authtoken now. How do I get the accesstoken, refreshtoken…?
I cant find the url for requesting it with the authtoken.

Im using OAuth Implicit Code Flow.

Or am I on the complete wrong way?

response_type code is for OAuth Authorization Code Flow not implicit

Yeah I changed that to response_type=token

Okay,
so I tried to change some things. For example downgrading to kraken API getting credentials and stuff from the kraken API(https://api.twitch.tv/kraken/oauth2/token?, …).
I still get the same error like mentioned above.

Step 2) Exchange the returned code for a access token aka auth token (they are one and the same)

What do you excactly mean with that?
Currently I:

  • Send my user to auth https://api.twitch.tv/kraken/oauth2/authorize?
  • Get the code from the URL Parameters and requesting tokens https://api.twitch.tv/kraken/oauth2/token
  • { access_token: ‘REMOVED’,
    refresh_token: ‘REMOVED’,
    scope: [ ‘user_read’, ‘user_subscriptions’ ],
    token_type: ‘bearer’ } save that to the database
  •         let data = {
              "type": "LISTEN",
              data: {
                  "topics": ["channel-subscribe-events-v1.44322889"],
                  "auth_token": accessToken
              }
          }
          socket.send(JSON.stringify(data));
    

And connect to the socket

{“type”:“RESPONSE”,“error”:“ERR_BADAUTH”,“nonce”:""}

I still get that error.

You shouldn’t leak any part of a token. Don’t shove XXXXX in it just remove it all.

What you are doing looks correct. But is the access token for user 44322889?

No, thats the example url. My bad.
I changed it to the correct userId and it still gives me the same error.
I requested the userId from the (https://api.twitch.tv/kraken/user) endpoint

Edit: Sry my pc didnt show the post

I’m out of ideas here.

It’s working perfectly fine for me.

But your code is missing a nonce: for example:

{
  "type": "LISTEN",
  "nonce": "44h1k13746815ab1r2",
  "data": {
    "topics": ["channel-bits-events-v1.44322889"],
    "auth_token": "cfabdegwdoklmawdzdo98xt2fo512y"
  }
}

from

Which yes is optional. But it’s the only difference between my code and your code that I can see

Try checking the validity of your token https://dev.twitch.tv/docs/authentication/#validating-requests

Make sure that the token is valid, has the correct scope, and the user id in the token matches that of the user id in the topic you’re attempting to listen to on Pubsub.

I send a validate rqeuest and got the right information. Userid is the same as in the listen message send to the Pubsub.

Well I finally found the error.
I needed to switch to the kraken API in order to use it and I had the ‘user_subscriptions’ instead of ‘channel_subscriptions’ scope…

Thank you all for your help!