如何设置Twitch电视频道标题

时间:2015-10-08 21:28:54

标签: c# api curl restsharp twitch

我正在努力让我的自定义聊天机器人更新我的主要频道的标题(状态)。我正在关注this帖子,我正试图从access_token获取REDIRECT_URI

包含重定向的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);

这是一个控制台应用程序

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

This twitch dev post帮助了我,直到我进入“提出请求”步骤。我的问题是我需要使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 ctrl + v从身份验证请求中手动获取access_token 来自下面给出的URI的令牌并将其存储到我的数据库中:

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

然后,我使用Postman使用JSON中的正文请求生成我的RestSharp代码:

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