Quantcast
Channel: Wangdora Eve.
Viewing all articles
Browse latest Browse all 117

運営コメント送信APIの実装

$
0
0

C# / ニコ生の運営コメント送信APIの実装

ライブラリを追加

 Web APIのクライアントに必要なライブラリは基本的にASP.NET MVC 4に含まれていますが、依存関係もそれなりにあるので、NuGetを使用した方が楽です。
 GUIを使用する場合には下図のように”Microsoft ASP.NET Web API Client Libraries”を選択し、インストールします。




usingの追加

using System.Net.Http;
using System.Net.Http.Formatting; // 参照だけでok
using System.Runtime.Serialization;


データクラスの追加

[DataContract]
public class PostComment
{
[DataMember(Name = "text")]
public string text { get; set; }
[DataMember(Name = "isPermanent")]
public bool isPermanent { get; set; }
[DataMember(Name = "color")]
public string color { get; set; }
[DataMember(Name = "userName")]
public string userName { get; set; }
}

HTTP リクエスト (methodがPUTなのでPutAsJsonAsyncを使う。非同期。)

using (HttpClientHandler handler = new HttpClientHandler())
using (HttpClient client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(10) })
{
client.DefaultRequestHeaders.Add("User-Agent", "クライアント名");
handler.CookieContainer = _container;
PostComment _PostComment = new PostComment();
_PostComment.text = _Message;
_PostComment.isPermanent = _isPermanent;
_PostComment.color = "";
_PostComment.userName = "";
var response = await client.PutAsJsonAsync<PostComment ("http://live2.nicovideo.jp/watch/" + LiveID + "/operator_comment", _PostComment);
}

User-Agent : クライアントを判別できるものを付けてください。
container
: CookieContainer
Text : コメントテキスト
isPermanent : /permでずっと表示するコメにするかどうか(true/false)
color : コメントの色。red,blue,greenなど。省略可能。
userName : コテハン。省略可能。
LiveID : lvを含む放送番号

基本的な情報は下記を参考。
大百科の運コメAPI

.Net Framework 4.5以上必須
※覚書なのでご自由に。

Viewing all articles
Browse latest Browse all 117

Latest Images

Trending Articles