テキストを簡単に共有するサイトの中でも有名なPastebin.comというサイトがあります。
そちらにてAPIが公開されているみたいなので実装してみた。
APIキーを取得
まずAPIのキーを取得してきます。
当然ですがAPIを使うにはPastebinのアカウントが必要になります。
ページの序盤の方に(Pastebin.comにログインしていたら)APIキーが表示されます。
このキーを使用します。

ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
const string URL = @"http://pastebin.com/api/api_post.php"; const string Key = @"APIKEY"; string text = ""; string path = @"C:\test.txt"; using (StreamReader sr = new StreamReader(path)) text = sr.ReadToEnd(); NameValueCollection IQuery = new NameValueCollection(); IQuery.Add("api_dev_key", Key); IQuery.Add("api_option", "paste"); IQuery.Add("api_paste_code", "text"); // text(None), CSharp(C#) IQuery.Add("api_paste_private", "0"); // 0(Public), 1(Unlisted) IQuery.Add("api_paste_name", "Untitled"); IQuery.Add("api_paste_expire_date", "N"); // N(無期限)/10M(10分)/1H(1時間)/1D(1日)/1W(1週間)/2W/1M(1ヶ月) IQuery.Add("api_paste_format", type); using (WebClient IClient = new WebClient()) { string IResponse = Encoding.UTF8.GetString(IClient.UploadValues(URL, IQuery)); Uri isValid = null; if (!Uri.TryCreate(IResponse, UriKind.Absolute, out isValid)) { throw new WebException("Paste Error", WebExceptionStatus.SendFailure); } return IResponse; } } |
PastebinのAPIの呼出は非常に簡単で、WebClientでも動作するほどです。
APIの戻り値はURLだけなので、返り値がURLに変換できない場合はAPIエラーが発生しているということです。