0

I am trying to get a list of users who are subscribed to a twitch channel through the twitch API, I have setup the request to get the access token and then I am calling the API /helix/subscriptions/ passing in the access token to get the list of subscribers, however I keep running into the same issue where I can't get a successful request.

Request failed with status code Unauthorized

var options = new RestClientOptions("https://id.twitch.tv/oauth2/token")
{
    ThrowOnAnyError = true,
    Timeout = 1000
};

var client = new RestClient(options);

var request = new RestRequest()
    .AddQueryParameter("grant_type", "client_credentials")
    .AddQueryParameter("client_id", clientId)
    .AddQueryParameter("client_secret", clientSecret);

var response = await client.PostAsync(request);
var result = JsonConvert.DeserializeObject<OauthToken>(response.Content);

var userOptions = new RestClientOptions("https://api.twitch.tv/helix/subscriptions");

var clientR2 = new RestClient(userOptions);

var requestR2 = new RestRequest()
    .AddHeader("Authorization", "Bearer " + result.access_token)
    .AddHeader("Client-Id", clientId)
    .AddQueryParameter("broadcaster_id", broadcasterId);

var responseR2 = await clientR2.GetAsync(requestR2);

I am not sure not if this is possible, I have gone over most of the twitch API documents and I've got a similar setup working with just getting "users", but subscriptions seems to not be working even with similar parameters.

1 Answers1

1

You are using the wrong oAuth flow.

You need a token that representes a user, client_credentials broadly speaking doesn't represent a user but a "server".

So with your token you do not have permission to read broadcasterId subscriptions. Hence the 403/Request failed with status code Unauthorized

Read more on the official documentation here: https://dev.twitch.tv/docs/authentication#user-access-tokens

You need a response_type code or token not client_credentials

Barry Carlyon
  • 1,039
  • 9
  • 13
  • Okay so by the sounds of it, what I am trying to do can't be achieved? I am trying to setup an automatic program which will download the subscribers every hour or so – Sythnet Partridge May 02 '22 at 11:13
  • You can do this. You get a user token (and a refresh token). Then use the user token till it expires. Then use the refresh token to get a new access token. I do this _daily_ I use $streamersToken that I work for to resync their subscribers from the API into my DB. If the token is dead, I use the refresh token and away we go – Barry Carlyon May 02 '22 at 12:37