[EventSub WS] 400 Bad Request - The value specified in the method field is not valid

Hi, I am getting this error when trying to subscribe to any events.
Here is the code I am using for the subscription:

function subscribeToEventSub(event_name: string, version: number, condition: any = {}, app_token: string, session_id: string): void {
    fetch('https://api.twitch.tv/helix/eventsub/subscriptions', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + app_token,
            "Client-Id": "<APP CLIENT ID>",
            "type": event_name,
            "version": version.toString(),
            "condition": JSON.stringify(condition), // {"broadcaster_user_id": user_id}
            "transport": JSON.stringify({
                "method": "websocket",
                "session_id": session_id
            })
        }
    })
    ...
}

app_token is obtained using this piece of code:

async function getTwitchAppAccessToken(): Promise<string> {
    let req = await fetch(`https://id.twitch.tv/oauth2/token`
                          + `?client_id=<APP CLIENT ID>`
                          + `&client_secret=<APP CLIENT SECRET>`
                          + `&grant_type=client_credentials`,
    {
        method: 'POST'
    });
    let resp = await req.json();
    return resp["access_token"]; // pardon the lack of error checking
}

It is very likely that my error is linked to this:

If you use WebSockets to receive events, the request must specify a user access token

So I also tried replacing the Authorization token with one gotten from the implicit grant flow.
Here is the url I am using (I successfully manage to get the asked tokens, no problems here):

`https://id.twitch.tv/oauth2/authorize`
+ `?client_id=<APP CLIENT ID>`
+ `&redirect_uri=http://localhost:3000/callback`
+ `&response_type=token+id_token`
+ `&scope=channel:read:subscriptions+moderator:read:followers+bits:read+openid`

But with the Authorization token set to use the one from the implicit grant flow, I get another error:

The ID in the Client-Id header must match the client ID in the access token.

What am I doing wrong ? :sob:

Most of your Subscription request is in a header isntead of the body
And you have a slew of extra JSON stringify’s

Should be more like:

function subscribeToEventSub(event_name: string, version: string, condition: any = {}, app_token: string, session_id: string): void {
    fetch('https://api.twitch.tv/helix/eventsub/subscriptions', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + app_token,
            "Client-Id": "<APP CLIENT ID>"
       },
       body: JSON.stringify({
            "type": event_name,
            "version": version,
            "condition": condition, // {"broadcaster_user_id": user_id}
            "transport": {
                "method": "websocket",
                "session_id": session_id
            }
        }
    })

Also note that version in the function call should be typecast to a string so you can handle version beta and Twitch hasn’t said they’ll always be numeric in style.

You can refer to my code example here which might help you out:

https://barrycarlyon.github.io/twitch_misc/eventsub/websockets/web/basic/

Yeah as you noted, you are using the wrong token type.

WebSockets only accepts a user token.

Sounds like you generated an implict auth token with a different clientID to the one you tried to use in code

Thank you for the quick answer, will try it out!

Alright, good news is I am no longuer seeing the “The ID in the Client-Id header must match the client ID in the access token.” error.

The bad news is that even after implementing your changes and using a user token, I still get the “The value specified in the method field is not valid” error.

Just to be sure, when refering to “user token”, we are refering to the “access_token” fragment parameter sent to the callback url, correct?

For implict auth yes, the access token is in the access_token fragment

What is you code currently looking like?

My bad, I forgot to remove the damned JSON.stringify.
Everything is working and I am getting an actual response, thank you very much!

1 Like

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