Search code examples
c#apicurlrestsharptwitch

How to set Twitch TV channel title


I am working on being able to have my custom chat bot to update my primary channel's title (status). I am following this post and I am trying to get the access_token from the REDIRECT_URI.

The URI that contains the redirect is:

https://api.twitch.tv/kraken/oauth2/authorize?response_type=token&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=channel_editor

I have manually tested this with my CLIENT_ID and REDIRECT_URI set to http://localhost and I get this response from the above URI (which is what I want):

http://localhost/#access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&scope=channel_editor

I am trying to get the access_token from this URI, but I cant seem to get to it from the code below. My response is:

https://api.twitch.tv/kraken/oauth2/authenticate?action=authorize&client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&redirect_uri=http%3A%2F%2Flocalhost&response_type=token&scope=channel_editor

Code:

string clientID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string redirectURL = "http://localhost";

string url = string.Format("https://api.twitch.tv/kraken/oauth2/authorize?response_type=token&client_id={0}&redirect_uri={1}&scope=channel_editor",
                 clientID, redirectURL);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string redirUrl = response.Headers["Location"];
response.Close();

// Show the redirected url
Console.WriteLine("You're being redirected to: " + redirUrl);

This is a console application


Solution

  • This twitch dev post helped me out until I got to the "Make the request" step. My problem was I needed to make the C# equivalent of this cURL command to change the channel's title:

    curl -H 'Accept: application/vnd.twitchtv.v2+json' 
         -H 'Authorization: OAuth <access_token>' 
         -d "channel[status]=New+Status!&channel[game]=New+Game!" 
         -X PUT https://api.twitch.tv/kraken/channels/CHANNEL
    

    Solution:

    I decided to manually get the access_token from the authentication request by ctrl + c and ctrl + v the token from the URI given below and store it into my database:

    http://localhost/#access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&scope=channel_editor
    

    Then, I used Postman to generate my RestSharp code with the body request in JSON:

    string accessToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    var client = new RestClient("https://api.twitch.tv/kraken/channels/CHANNEL_NAME");
    var request = new RestRequest(Method.PUT);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("authorization", "OAuth " + accessToken);
    request.AddHeader("accept", "application/vnd.twitchtv.v3+json");
    request.AddParameter("application/json", "{\"channel\":{\"status\":\"Hello World\"}}", 
        ParameterType.RequestBody);
    
    IRestResponse response = client.Execute(request);