Search code examples
c#httptwitch

Why is the Twitch API giving me an undocumented HTTP 404 error on Check User Subscription By Channel?


When trying to check whether a user is subscribed to a particular channel using the Twitch API v5, I'm getting an HTTP 404 response from the twitch server. According to the documentation that should not happen in v5. v3 used to respond with an http 404 if the user wasn't subscribed but v5 should always respond with a json payload. If the user is not subscribed said payload would have a "status" field with value 404.

I am using the URL provided by twitch's documentation: https://api.twitch.tv/kraken/users/012345678/subscriptions/876543210

I am correctly connectiong to twitch since the API requests performed immediately before this one to get authorization from the user and their userid all work.

I have tested this both with users that are and users that aren't subscribed to the channel in question; both cause the same reaction.

The access token used to authorize the operation has scopes user_subscription and user_read.

In which situations does the twitch API respond like this?

Below is the code that is producing this problem:

twitchRequest = WebRequest.Create("https://api.twitch.tv/kraken/users/" + userId + "/subscriptions/" + DB.GetCode("TwitchChannelId"));
twitchRequest.Method = "GET";
twitchRequest.Headers.Add("Authorization: OAuth " + accessToken);
twitchRequest.Headers.Add("Client-ID: " + DB.GetCode("TwitchClientId"))
//this throws a System.Net.WebException
//The remote server returned an error: (404) Not Found.
twitchResponse = twitchRequest.GetResponse(); 

Solution

  • You're requesting the default v3 of the API, which uses usernames instead of IDs. To use v5, add

    twitchRequest.Headers.Add("Accept: application/vnd.twitchtv.v5+json");