"Invalid OAuth token" when creating an EventSub subscription

Hi! So I have working applications made in Unity C# that makes use of Twitchlib, PubSub, and HTTP API calls to connect to the user’s channel, chat, and manage channel point redemptions. Since PubSub is to be shutdown soon, I’ve spent the past month+ trying to port to EventSub (I’m a game dev with 0 networking knowledge, relying a lot on Hellcat’s example here).

I’ve managed to open an EventSub WebSocket connection and receive the welcome message, but trying to create a subscription results in an error 401: Invalid OAuth token.

I’ve validated the token that gets generated using Barry’s token generator example (thanks Barry!) and it seems to be valid.

I use the generated token to access chat via IRC and it works (can I use one token for both things?)

private System.Collections.IEnumerator CreateEventSubSubscriptionViaAPI()
{
string accessToken = Secrets.access_token;
string clientID = Secrets.client_id;
string broadcasterId = Secrets.broadcaster_id;
string subscriptionType = “channel.channel_points_custom_reward_redemption.add”;

    if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(clientID) || string.IsNullOrEmpty(sessionID) || string.IsNullOrEmpty(broadcasterId))
    {
        Debug.LogError($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Missing required parameters. Subscription API call cannot be made.");
        yield break;
    }

    EventSubSubscriptionRequest subscriptionRequest = new EventSubSubscriptionRequest
    {
        type = subscriptionType,
        condition = new EventSubCondition { broadcaster_user_id = broadcasterId },
        transport = new EventSubTransport { session_id = sessionID }
    };
    string requestBodyJson = JsonUtility.ToJson(subscriptionRequest);

    string apiUrl = "https://api.twitch.tv/helix/eventsub/subscriptions";

    string[] headers = new string[]
    {
    $"Client-Id: {clientID}",
    $"Authorization: Bearer {accessToken}"
    };

    Debug.Log($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Sending Subscription API Request via TwitchApiCallHelper with access token {accessToken}");

    Task<string> apiCallTask = gameObject.GetComponent<TwitchApiCallHelper>().CallApi(apiUrl, "POST", requestBodyJson, headers);
    yield return new WaitUntil(() => apiCallTask.IsCompleted);

    string apiResponseJson = apiCallTask.Result;

    if (!string.IsNullOrEmpty(apiResponseJson))
    {
        Debug.Log($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Subscription API call Response: {apiResponseJson}");

        if (apiCallTask.IsFaulted || apiResponseJson.Contains("\"error\":"))
        {
            Debug.LogError($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Subscription API call failed!");
        }
        else
        {
            Debug.Log($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Subscription API call likely successful");
        }
    }
    else
    {
        Debug.LogError($"[{DateTime.Now:HH:mm:ss.fff}] TwitchEventManager: CreateEventSubSubscriptionViaAPI() - Subscription API call failed! Empty response received.");
    }
}

C# objects for json serialization

[System.Serializable]
public class EventSubCondition
{
public string broadcaster_user_id;
}

[System.Serializable]
public class EventSubTransport
{
public string method = “websocket”;
public string session_id;
}

[System.Serializable]
public class EventSubSubscriptionRequest
{
public string type;
public string version = “1”;
public EventSubCondition condition;
public EventSubTransport transport;
}

The TwitchAPICallHelper is straight out of Hellcat’s example.

I get the feeling it’s something silly but man, I just can’t see it

It was indeed something dumb - I managed to double up my headers in the subscription creation call

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