1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Text.RegularExpressions;
25: using Utilities.Web.OAuth;
26:
27: #endregion
28:
29: namespace Utilities.Web.Twitter
30: {
31: /// <summary>
32: /// Helper class to deal with Twitter
33: /// </summary>
34: public class Twitter : OAuth.OAuth
35: {
36: #region Constructor
37:
38: /// <summary>
39: /// Constructor
40: /// </summary>
41: public Twitter()
42: : base()
43: {
44: this.SignatureType = Signature.HMACSHA1;
45: }
46:
47: #endregion
48:
49: #region Public Functions
50:
51: /// <summary>
52: /// Gets a request token/token secret
53: /// </summary>
54: /// <param name="Token">The request token</param>
55: /// <param name="TokenSecret">The request token secret</param>
56: public void GetRequestToken(out string Token, out string TokenSecret)
57: {
58: this.Token = "";
59: this.TokenSecret = "";
60: this.Method = HTTPMethod.GET;
61: this.Url = new Uri("http://twitter.com/oauth/request_token");
62: REST.REST RestHelper = new Utilities.Web.REST.REST();
63: RestHelper.Url = new Uri(GenerateRequest());
64: string Value = RestHelper.GET();
65: Regex TokenRegex = new Regex("oauth_token=(?<Value>[^&]*)");
66: Match TempToken = TokenRegex.Match(Value);
67: Token = TempToken.Groups["Value"].Value;
68: Regex TokenSecretRegex = new Regex("oauth_token_secret=(?<Value>[^&]*)");
69: Match TempTokenSecret = TokenSecretRegex.Match(Value);
70: TokenSecret = TempTokenSecret.Groups["Value"].Value;
71: }
72:
73: /// <summary>
74: /// Gets the location of the authorization site (requires
75: /// request token/token secret)
76: /// </summary>
77: /// <returns>The location that the user must go in order to
78: /// authorize the app and get the authorization PIN</returns>
79: public string GetAuthorizationSite()
80: {
81: this.Method = HTTPMethod.GET;
82: this.Url = new Uri("https://twitter.com/oauth/authorize");
83: this.AddParameter("oauth_callback", "oob");
84: return new Uri(GenerateRequest()).ToString();
85: }
86:
87: /// <summary>
88: /// Gets the access token/token secret which are used in actual calls
89: /// (requires the PIN from the authorization site and the request token
90: /// and request token secret)
91: /// </summary>
92: /// <param name="PIN">PIN received from the authorization site</param>
93: /// <param name="AccessToken">The access token</param>
94: /// <param name="AccessTokenSecret">The access token secret</param>
95: public void GetAccessToken(string PIN, out string AccessToken, out string AccessTokenSecret)
96: {
97: this.Url = new Uri("https://twitter.com/oauth/access_token");
98: this.AddParameter("oauth_verifier", PIN);
99: this.Method = HTTPMethod.POST;
100: REST.REST RestHelper = new Utilities.Web.REST.REST();
101: RestHelper.Url = new Uri(GenerateRequest());
102: string Value = RestHelper.POST();
103: Regex TokenRegex = new Regex("oauth_token=(?<Value>[^&]*)");
104: Match TempToken = TokenRegex.Match(Value);
105: AccessToken = TempToken.Groups["Value"].Value;
106: Regex TokenSecretRegex = new Regex("oauth_token_secret=(?<Value>[^&]*)");
107: Match TempTokenSecret = TokenSecretRegex.Match(Value);
108: AccessTokenSecret = TempTokenSecret.Groups["Value"].Value;
109: }
110:
111: /// <summary>
112: /// Updates the status of the user
113: /// </summary>
114: /// <param name="Status">Status of the user (needs to be within 140 characters)</param>
115: /// <returns>The XML doc returned from the Twitter service</returns>
116: public string UpdateStatus(string Status)
117: {
118: this.Method = HTTPMethod.POST;
119: this.Url = new Uri("http://twitter.com/statuses/update.xml");
120: this.AddParameter("status", Status);
121: REST.REST RestHelper = new Utilities.Web.REST.REST();
122: RestHelper.Url = new Uri(GenerateRequest());
123: return RestHelper.POST();
124: }
125:
126: /// <summary>
127: /// Gets a user's timeline from the Twitter service
128: /// </summary>
129: /// <param name="UserName">The screen name of the user</param>
130: /// <returns>The XML doc returned from the Twitter service
131: /// contatining the timeline</returns>
132: public string GetTimeline(string UserName)
133: {
134: this.Method = HTTPMethod.POST;
135: this.Url = new Uri("http://twitter.com/statuses/user_timeline.xml");
136: this.AddParameter("screen_name", UserName);
137: REST.REST RestHelper = new Utilities.Web.REST.REST();
138: RestHelper.Url = new Uri(GenerateRequest());
139: return RestHelper.GET();
140: }
141:
142: #endregion
143: }
144: }