Get polls from API : error 401 : unauthorized

Hello,

First, sorry for my poor english…

Im my app, I get the current viewer count with no problem, and I also get the schedule.

I wanted to add another call to the API go get my polls, but I always get an 401 error. I supposed I didn’t define my scope correctly.

I try to ask for token then send my get request for polls. Is anyone have an idea what am I doing wrong ?

        string Token = string.Empty;
                //Get token :
                var UrlOAuth = "https://id.twitch.tv/oauth2/token?client_id=my_client_id&client_secret=my_client_secret&grant_type=client_credentials&scope=channel%3Aread%3Apolls";

                var httpRequest = (HttpWebRequest)WebRequest.Create(UrlOAuth);
                httpRequest.Method = "POST";
                try
                {

                    var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JToken>(result);
                        Token = obj["access_token"].ToString();
                        //so far, it works, I get my token
                        if (!string.IsNullOrWhiteSpace(Token))
                        {
                            var urlStream = $"https://api.twitch.tv/helix/polls?broadcaster_id=my_broadcast_id";
                            var httpReqStreamPoll = (HttpWebRequest)WebRequest.Create(urlStream);
                            httpReqStreamPoll.Method = "GET";
                            httpReqStreamPoll.Headers.Add("Authorization", string.Format("Bearer {0}", Token));
                            httpReqStreamPoll.Headers.Add("Client-Id", "my_client_id");
                            var httpResStreamPoll = (HttpWebResponse)httpReqStreamPoll.GetResponse();
                            using (var strReaderPoll = new StreamReader(httpResStreamPoll.GetResponseStream()))
                            {
                                var resStream = strReaderPoll.ReadToEnd();
                            }
                        }
                    }
                }

Thank you very much in advance !

You are generating a client credentials token

This kind of token cannot have scopes
This kind of token cannot be used to read poll information

You need a user token. And that user token needs to belong to the user that you want to read the polls of

So to read ninja’s polls you need to get a token from ninja for example

And to read my polls you need a token from me

So follow the user token flow and ask the broadcaster to authenticate to allow access to their polls

You should also read the body of the response as the error message will have noted this information. But you only read the HTTP code and not the error JSON that was also returned

Thank you for your aswer ! (sorry for long delay)

I try this !!!

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