My first post: why is Oauth required to post to my own twitch chat?

All I want to do is post chat messages to my own twitch chat, using my own program that I wrote with c#.

But when I look at the the API documentation for nightbot, I’ve literally read and re-read it about 5 times trying to make sense of what on earth it is asking me to do.

  1. Click a few buttons to register my app, and get some cient and secret keys: DONE. This part makes sense to me.

  2. Register redirect urls: Wha?? Huh??? Makes no sense, so I skipped this part… I just want to post to my twitch chat… why do I need redirect urls for that?

  3. And now it just gets weirder and weirder… now it tells me I must post to some authorization page, and request a tokin which grants access to “a user” for 30 days, and save that tokin, and refresh that tokin, and bla bla blah blah… and oh, BY THE WAY, I must select from one of my “redirect urls” or else the whole request fails!!! AAAAAHHHHH.

I just want to post some messages to my twitch chat, why is this so complicated. :frowning:

Honestly, it should be enough to just supply my client and secret key, and a message I want to post, and boom it should post it. I don’t see why any additional trouble should be necessary.

First you should get familiar with how normal OAuth2 works for applications as many other services use this method of authorization. Here are some cool guides Google’s, DigitalOcean’s.

If you’re building your own application you don’t need to go through the process of creating a redirection URL. You can simply authorize yourself shown in this post. This will give your API Access Token that you use in every endpoint.

Hmm, I’m trying to post to the url the person on that page suggested, and no matter what it always says 404 error “not found”.

Link I’m posting to:{my_client_id}&client_secret={my_client_secret}

So I thought, maybe it wants those values to be included in the header instead? So I tried doing it with c# and used header values to send those values… and it still gives the same error, 404 “not found”. Kind of hard to understand what’s the matter, that error code isn’t very helpful. Any idea what I’m doing wrong? Or is it possible that this method of authorization doesn’t work anymore?

I got past that 404 error… I was not posting it with “POST” method. Once I made sure I was using post method, it now quit giving me that error, but now it give me error 400 “bad request”. I’ve tripple checked my client and secret keys, everything is correct. Tried sending it with both header values, and with non-header values, and both ways it’s the same 400 “bad request” response.

It sounds like you are sending the grant_type, client_id, and client_secret parameters as query string parameters. You should be sending them as body parameters.

@am_1 Thanks for the reply but how do you know this? I haven’t seen anything anywhere say that it needs to be posted as body parameters. I mean, if it is body, then how do you seperate each value? With a new line character?

Honestly, the next person who comes on here is gonna say that I’m supposed to upload the values as a PDF file.

I know this because in the docs, the examples show body parameters. Also, in the post from night that Aaron linked, there was no question mark before the parameters began. Also, POST requests generally require body parameters.

No, you don’t separate each value with a line character. Whatever class you’re using to perform the request should have a method for adding body parameters.

Since you’re using C#, I recommend RestSharp. It makes this very easy:

var client = new RestClient("");

var tokenRequest = new RestRequest("oauth2/token", Method.POST);
tokenRequest.AddParameter("grant_type", "client_credentials");
tokenRequest.AddParameter("client_id", "YOUR_CLIENT_ID");
tokenRequest.AddParameter("client_secret", "YOUR_CLIENT_sECRET");
tokenRequest.AddParameter("scope", "channel_send");

var tokenResponse = await client.ExecuteTaskAsync<TokenResponse>(tokenRequest);
if (tokenResponse.Data?.AccessToken is null)
    // Deal with error

var sendChannelMessageRequest = new RestRequest("1/channel/send", Method.POST);
sendChannelMessageRequest.AddHeader("Authorization", $"Bearer {tokenResponse.Data.AccessToken}");
sendChannelMessageRequest.AddParameter("message", "test message");

var sendChannelMessageResponse = await client.ExecuteTaskAsync<SendChannelMessageResponse>(sendChannelMessageRequest);
if (sendChannelMessageResponse.Data?.StatusCode != 200)
    // Deal with error

Response classes:

class TokenResponse
    [DeserializeAs(Name = "access_token")]
    public string AccessToken { get; set; }
    [DeserializeAs(Name = "token_type")]
    public string TokenType { get; set; }
    [DeserializeAs(Name = "expires_in")]
    public int? ExpiresIn { get; set; }
    [DeserializeAs(Name = "refresh_token")]
    public string RefreshToken { get; set; }
    [DeserializeAs(Name = "scope")]
    public string Scope { get; set; }
    [DeserializeAs(Name = "message")]
    public string ErrorMessage { get; set; }
    [DeserializeAs(Name = "code")]
    public int? ErrorCode { get; set; }
    [DeserializeAs(Name = "name")]
    public string ErrorName { get; set; }

class SendChannelMessageResponse
    [DeserializeAs(Name = "status")]
    public int? StatusCode { get; set; }
    [DeserializeAs(Name = "message")]
    public string ErrorMessage { get; set; }

I did try RestSharp, but it send me backwards in terms of progress, and I was back to the 404 error even though I was setting the method type to post.

Anyway, I finally got it working. In case anyone else has my same problem, here’s the exact and entire code I used to do it:

WebClient WC2 = new WebClient();
WC2.Headers[HttpRequestHeader.ContentType] = “application/x-www-form-urlencoded”;
var data = new System.Collections.Specialized.NameValueCollection();
data.Add(“client_id”, YOUR_CLIENT_ID);
data.Add(“client_secret”, YOUR_CLIENT_SECRET);
data.Add(“grant_type”, “client_credentials”);
tring nightbotWC = Encoding.UTF8.GetString(WC.UploadValues(“”, data));

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