Hi, I am creating a game where you can connect your Twitch account to an application I created. In order to read the chat I am trying to subscribe to an EventSub using a WebSocket but something do not allow me to do this.
- I authorize the app with some scopes (channel:bot, user:bot, user:read:chat, user:write:chat)
- I get a user access token using my Twitch channel
- I validate the token (this step will be the first if there is already a saved token which is not expired)
- I get the id of the broadcaster (my channel) and the id of the app
- I open the websocket to the url wss://eventsub.wss.twitch.tv/ws
- I receive the welcome message
- I try to subscribe the EventSub with the url https://api.twitch.tv/helix/eventsub/subscriptions
This last step is the one that do not let me connect. Here is the piece of code:
async Task<bool> EventSubSubscription(string token, string clientId, string sessionId, string broadcasterUserId, string userId) {
string body;
ApiRespose_EventSub apiResponseData;
ApiRequest_EventSub apiRequestData;
HttpRequestMessage httpRequest;
string returnData;
httpClient.BaseAddress = null;
httpClient.DefaultRequestHeaders.Clear();
httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.twitch.tv/helix/eventsub/subscriptions");
httpRequest.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);
httpRequest.Headers.TryAddWithoutValidation("Client-Id", clientId);
httpRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");
apiRequestData = new ApiRequest_EventSub
{
type = "channel.chat.message",
version = "1",
condition = new ApiRequest_EventSub_Condition
{
broadcaster_user_id = broadcasterUserId,
user_id = userId,
},
transport = new ApiRequest_EventSub_Transport
{
method = "websocket",
session_id = sessionId,
},
};
body = JsonUtility.ToJson(apiRequestData);
httpRequest.Content = new StringContent(body, Encoding.UTF8, "application/json");
// send request and wait for it to complete
Task<HttpResponseMessage> httpRespose = httpClient.SendAsync(httpRequest);
while (!httpRespose.IsCompleted) {
await Task.Delay(10);
}
// fetch response content
Task<string> httpResponseContent = httpRespose.Result.Content.ReadAsStringAsync();
while (!httpResponseContent.IsCompleted) {
await Task.Delay(10);
}
// return the response content and be done - calling an API can be so easy :-D
returnData = httpResponseContent.Result;
// parse the return JSON into a more usable data object
apiResponseData = JsonUtility.FromJson<ApiRespose_EventSub>(returnData);
Debug.Log(returnData);
try {
return apiResponseData.data[0].condition.broadcaster_user_id == broadcasterUserId;
} catch (Exception e) {
Debug.LogError(e);
return false;
}
}
The errore message that appears is {“error”:“Forbidden”,“status”:403,“message”:“subscription missing proper authorization”}
I do not understand what I am doing wrongly, so I ask you for help.
Thank you in advance.