0

如何登录推特并查看通知?

Facebook

对于 facebook,我使用这部分代码进行登录

wb1.Document.GetElementById("email").InnterText="user";

因为代码html id

<input type="text" class="inputtext" name="email" id="email" value="" tabindex="1" data-dynamite-selected="true">

检查通知我用这个

HtmlElement ID1 = wb1.Document.GetElementById("notificationsCountValue");

推特

但在 twitter 页面我有这个 html 代码

<div class="placeholding-input username hasome">
                <input type="text" class="text-input email-input" name="session[username_or_email]" title="Nome utente o email" autocomplete="on" data-dynamite-selected="true">
                  <span class="placeholder">Nome utente o email</span>
              </div>
4

1 回答 1

1

这是使用 TweetSharp 的示例。

首先,您需要访问https://dev.twitter.com/,使用您的 twitter 帐户登录并创建新应用程序以获取consumerKeyconsumerSecret值。

这是用于登录 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);
        }
    }
}
于 2012-07-10T10:30:40.593 回答