这是使用 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);
}
}
}