如何设置 Twitch 电视频道标题

How to set Twitch TV channel title

我正在努力让我的自定义聊天机器人能够更新我的主要频道的标题(状态)。我正在关注 this post,我正在尝试从 REDIRECT_URI.

获取 access_token

包含重定向的 URI 是:

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

我已经将我的 CLIENT_IDREDIRECT_URI 设置为 http://localhost 进行了手动测试,我从上面的 URI 得到了这个响应(这就是我想要的):

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

我正在尝试从此 URI 获取 access_token,但我似乎无法从下面的代码获取它。我的回复是:

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

代码:

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);

这是一个控制台应用程序

这与 c# 没有直接关系,但实现它应该很容易 https://discuss.dev.twitch.tv/t/how-to-set-title/390/2

This twitch dev post 帮助我完成了 "Make the request" 步骤。我的问题是我需要使 C# 等效于此 cURL 命令来更改频道的标题:

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

解决方案

我决定通过 ctrl + c 从身份验证请求中手动获取 access_token ctrl + v 下面给出的 URI 中的令牌并将其存储到我的数据库中:

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

然后,我在 JSON 中使用 Postman to generate my RestSharp 代码和 body 请求:

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);