Using Twitter Via .NET - A Beginner's Guide


SUBMITTED BY: Guest

DATE: June 12, 2013, 1:16 a.m.

FORMAT: C#

SIZE: 3.0 kB

HITS: 924

  1. private WebAuthorizer auth;
  2. private TwitterContext twitterCtx;
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. IOAuthCredentials credentials = new SessionStateCredentials();
  6. if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
  7. {
  8. credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
  9. credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
  10. }
  11. auth = new WebAuthorizer
  12. {
  13. Credentials = credentials,
  14. PerformRedirect = authUrl => Response.Redirect(authUrl)
  15. };
  16. if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
  17. {
  18. auth.CompleteAuthorization(Request.Url);
  19. }
  20. if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
  21. string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
  22. {
  23. //Do nothing
  24. }
  25. else if (auth.IsAuthorized)
  26. {
  27. //Do nothing
  28. }
  29. else
  30. {
  31. auth.BeginAuthorization(Request.Url);
  32. if (!auth.IsAuthorized)
  33. {
  34. Uri specialUri = new Uri(Request.Url.ToString());
  35. auth.BeginAuthorization(specialUri);
  36. }
  37. }
  38. if (auth.IsAuthorized)
  39. {
  40. twitterCtx = new TwitterContext(auth);
  41. #region Search Text
  42. var searchResult =
  43. (from srch in twitterCtx.Search
  44. where srch.Type == SearchType.Search &&
  45. srch.Query == "Brian Adams"
  46. select srch)
  47. .SingleOrDefault();
  48. gvTweetView.DataSource = searchResult.Statuses;
  49. gvTweetView.DataBind();
  50. #endregion
  51. #region UserInfo
  52. var users =
  53. (from user in twitterCtx.User
  54. where user.Type == UserType.Lookup &&
  55. user.ScreenName == "prosarfraz"
  56. select user);
  57. string strHtml = string.Empty;
  58. foreach (User objUser in users)
  59. {
  60. strHtml += "<TR>";
  61. strHtml += "<TD> Screen Name </TD>";
  62. strHtml += "<TD>" + objUser.ScreenName + "</TD>";
  63. strHtml += "<TD> Favorites Count </TD>";
  64. strHtml += "<TD>" + objUser.FavoritesCount + "</TD>";
  65. strHtml += "<TD> Followers </TD>";
  66. strHtml += "<TD>" + objUser.FollowersCount + "</TD>";
  67. strHtml += "<TD> Description </TD>";
  68. strHtml += "<TD>" + objUser.Description + "</TD>";
  69. strHtml += "</TR>";
  70. }
  71. ulsrc.InnerHtml = "<Table border ='1'>" + strHtml + "</Table>";
  72. #endregion
  73. }
  74. }

comments powered by Disqus