過去のブログのアーカイブ
この記事は前身のブログのアーカイブを引き継いだものです.
画像が正しく表示できないなど,コンテンツの表示に問題がある恐れがあります.テキストを簡単に共有するサイトの中でも有名なPastebin.comというサイトがあります。
そちらにてAPIが公開されているみたいなので実装してみた。
APIキーを取得
まずAPIのキーを取得してきます。
Pastebin.com API
当然ですがAPIを使うにはPastebinのアカウントが必要になります。
ページの序盤の方に(Pastebin.comにログインしていたら)APIキーが表示されます。
このキーを使用します。

ソースコード
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 (Post To Pastebin)
PastebinのAPIの呼出は非常に簡単で、WebClientでも動作するほどです。
APIの戻り値はURLだけなので、返り値がURLに変換できない場合はAPIエラーが発生しているということです。


