これは TweetSharp を使用した例です。
まず、https://dev.twitter.com/にアクセスし、Twitter アカウントでログインして、consumerKeyとconsumerSecretの値を取得するために新しいアプリケーションを作成する必要があります。
これは、Twitter にログインしてすべてのメンションを取得するためのヘルパー クラスです。このクラスは、HttpWebRequest/HttpWebResponse を介して自動的に認証コードを取得します。
public class Twitter
{
private TwitterService service;
private OAuthRequestToken requestToken;
public Twitter(String consumerKey, String consumerSecret)
{
service = new TwitterService(consumerKey, consumerSecret);
requestToken = service.GetRequestToken();
}
public void Login(String username, String password)
{
Uri uri = service.GetAuthorizationUri(requestToken);
CookieContainer cookieContainer = new CookieContainer();
String response = HttpUtil.GetResponseString(uri, cookieContainer);
int startIndex = response.IndexOf("authenticity_token\" type=\"hidden\" value=\"") + 41;
int endIndex = response.IndexOf("\"", startIndex + 1);
String authenticity_token = response.Substring(startIndex, endIndex - startIndex);
startIndex = response.IndexOf("name=\"oauth_token\" type=\"hidden\" value=\"") + 40;
endIndex = response.IndexOf("\"", startIndex + 1);
String oauth_token = response.Substring(startIndex, endIndex - startIndex);
String postData = "authenticity_token=" + authenticity_token +
"&oauth_token=" + oauth_token +
"&session%5Busername_or_email%5D=" + username +
"&session%5Bpassword%5D=" + password;
response = HttpUtil.GetResponseString(new Uri("https://api.twitter.com/oauth/authorize"), postData, cookieContainer);
if (response.Contains("Invalid user name or password"))
{
return;
}
startIndex = response.IndexOf("<code>") + 6;
endIndex = response.IndexOf("</code>");
String pin = response.Substring(startIndex, endIndex - startIndex);
OAuthAccessToken access = service.GetAccessToken(requestToken, pin);
service.AuthenticateWith(access.Token, access.TokenSecret);
}
public List<TwitterStatus> GetMentiones()
{
return service.ListTweetsMentioningMe().ToList<TwitterStatus>();
}
}
認証コードを自動的に取得するために、Twitter クラスで使用される GET および POST リクエストのヘルパー クラスを次に示します。
public static class HttpUtil
{
public static String GetResponseString(Uri url, CookieContainer cc)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String responseString = reader.ReadToEnd();
response.Close();
return responseString;
}
public static String GetResponseString(Uri url, String postData, CookieContainer cc)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = postData.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cc;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String responseString = reader.ReadToEnd();
response.Close();
return responseString;
}
}
最後に、簡単な例:
class Program
{
static void Main(string[] args)
{
Twitter twitter = new Twitter(**your_consumer_key**, **your_consumer_secret**);
twitter.Login(**username**, **password**);
List<TwitterStatus> statuses = twitter.GetMentiones();
foreach (TwitterStatus status in statuses)
{
Console.WriteLine(status.Text);
}
}
}