Mercurial > altnet-hispano
comparison Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs @ 90:d1688622fa88
Autenticando con twitter (falta emprolijar el código, pero autentica!)
author | Nelo@Kenia.neluz.int |
---|---|
date | Fri, 03 Jun 2011 21:35:59 -0300 |
parents | |
children | db4b1e2cae49 |
comparison
equal
deleted
inserted
replaced
89:24e9488ac152 | 90:d1688622fa88 |
---|---|
1 using System; | |
2 using System.Configuration; | |
3 using System.Web; | |
4 using System.Net; | |
5 using System.IO; | |
6 using System.Collections.Specialized; | |
7 | |
8 namespace AltNetHispano.Agendas.Twitter | |
9 { | |
10 public class oAuthTwitter : OAuthBase | |
11 { | |
12 public enum Method { GET, POST, DELETE }; | |
13 public const string REQUEST_TOKEN = "http://twitter.com/oauth/request_token"; | |
14 public const string AUTHORIZE = "http://twitter.com/oauth/authorize"; | |
15 public const string ACCESS_TOKEN = "http://twitter.com/oauth/access_token"; | |
16 | |
17 private string _consumerKey = ""; | |
18 private string _consumerSecret = ""; | |
19 private string _token = ""; | |
20 private string _tokenSecret = ""; | |
21 private string _callBackUrl = "oob"; | |
22 private string _oauthVerifier = ""; | |
23 | |
24 | |
25 #region Properties | |
26 public string ConsumerKey | |
27 { | |
28 get | |
29 { | |
30 if (_consumerKey.Length == 0) | |
31 { | |
32 _consumerKey = ConfigurationManager.AppSettings["consumerKey"]; | |
33 } | |
34 return _consumerKey; | |
35 } | |
36 set { _consumerKey = value; } | |
37 } | |
38 | |
39 public string ConsumerSecret { | |
40 get { | |
41 if (_consumerSecret.Length == 0) | |
42 { | |
43 _consumerSecret = ConfigurationManager.AppSettings["consumerSecret"]; | |
44 } | |
45 return _consumerSecret; | |
46 } | |
47 set { _consumerSecret = value; } | |
48 } | |
49 | |
50 public string Token { get { return _token; } set { _token = value; } } | |
51 public string TokenSecret { get { return _tokenSecret; } set { _tokenSecret = value; } } | |
52 public string CallBackUrl { get { return _callBackUrl; } set { _callBackUrl = value; } } | |
53 public string OAuthVerifier { get { return _oauthVerifier; } set { _oauthVerifier = value; } } | |
54 | |
55 #endregion | |
56 | |
57 /// <summary> | |
58 /// Get the link to Twitter's authorization page for this application. | |
59 /// </summary> | |
60 /// <returns>The url with a valid request token, or a null string.</returns> | |
61 public string AuthorizationLinkGet() | |
62 { | |
63 string ret = null; | |
64 | |
65 string response = oAuthWebRequest(Method.GET, REQUEST_TOKEN, String.Empty); | |
66 if (response.Length > 0) | |
67 { | |
68 //response contains token and token secret. We only need the token. | |
69 NameValueCollection qs = HttpUtility.ParseQueryString(response); | |
70 | |
71 if (qs["oauth_callback_confirmed"] != null) | |
72 { | |
73 if (qs["oauth_callback_confirmed"] != "true") | |
74 { | |
75 throw new Exception("OAuth callback not confirmed."); | |
76 } | |
77 } | |
78 | |
79 if (qs["oauth_token"] != null) | |
80 { | |
81 ret = AUTHORIZE + "?oauth_token=" + qs["oauth_token"]; | |
82 } | |
83 } | |
84 return ret; | |
85 } | |
86 | |
87 /// <summary> | |
88 /// Exchange the request token for an access token. | |
89 /// </summary> | |
90 /// <param name="authToken">The oauth_token is supplied by Twitter's authorization page following the callback.</param> | |
91 /// <param name="oauthVerifier">An oauth_verifier parameter is provided to the client either in the pre-configured callback URL</param> | |
92 public void AccessTokenGet(string authToken, string oauthVerifier) | |
93 { | |
94 this.Token = authToken; | |
95 this.OAuthVerifier = oauthVerifier; | |
96 | |
97 string response = oAuthWebRequest(Method.GET, ACCESS_TOKEN, String.Empty); | |
98 | |
99 if (response.Length > 0) | |
100 { | |
101 //Store the Token and Token Secret | |
102 NameValueCollection qs = HttpUtility.ParseQueryString(response); | |
103 if (qs["oauth_token"] != null) | |
104 { | |
105 this.Token = qs["oauth_token"]; | |
106 } | |
107 if (qs["oauth_token_secret"] != null) | |
108 { | |
109 this.TokenSecret = qs["oauth_token_secret"]; | |
110 } | |
111 } | |
112 } | |
113 | |
114 /// <summary> | |
115 /// Submit a web request using oAuth. | |
116 /// </summary> | |
117 /// <param name="method">GET or POST</param> | |
118 /// <param name="url">The full url, including the querystring.</param> | |
119 /// <param name="postData">Data to post (querystring format)</param> | |
120 /// <returns>The web server response.</returns> | |
121 public string oAuthWebRequest(Method method, string url, string postData) | |
122 { | |
123 string outUrl = ""; | |
124 string querystring = ""; | |
125 string ret = ""; | |
126 | |
127 | |
128 //Setup postData for signing. | |
129 //Add the postData to the querystring. | |
130 if (method == Method.POST || method == Method.DELETE) | |
131 { | |
132 if (postData.Length > 0) | |
133 { | |
134 //Decode the parameters and re-encode using the oAuth UrlEncode method. | |
135 NameValueCollection qs = HttpUtility.ParseQueryString(postData); | |
136 postData = ""; | |
137 foreach (string key in qs.AllKeys) | |
138 { | |
139 if (postData.Length > 0) | |
140 { | |
141 postData += "&"; | |
142 } | |
143 qs[key] = HttpUtility.UrlDecode(qs[key]); | |
144 qs[key] = this.UrlEncode(qs[key]); | |
145 postData += key + "=" + qs[key]; | |
146 | |
147 } | |
148 if (url.IndexOf("?") > 0) | |
149 { | |
150 url += "&"; | |
151 } | |
152 else | |
153 { | |
154 url += "?"; | |
155 } | |
156 url += postData; | |
157 } | |
158 } | |
159 | |
160 Uri uri = new Uri(url); | |
161 | |
162 string nonce = this.GenerateNonce(); | |
163 string timeStamp = this.GenerateTimeStamp(); | |
164 | |
165 //Generate Signature | |
166 string sig = this.GenerateSignature(uri, | |
167 this.ConsumerKey, | |
168 this.ConsumerSecret, | |
169 this.Token, | |
170 this.TokenSecret, | |
171 this.CallBackUrl, | |
172 this.OAuthVerifier, | |
173 method.ToString(), | |
174 timeStamp, | |
175 nonce, | |
176 out outUrl, | |
177 out querystring); | |
178 | |
179 querystring += "&oauth_signature=" + this.UrlEncode(sig); | |
180 | |
181 //Convert the querystring to postData | |
182 if (method == Method.POST || method == Method.DELETE) | |
183 { | |
184 postData = querystring; | |
185 querystring = ""; | |
186 } | |
187 | |
188 if (querystring.Length > 0) | |
189 { | |
190 outUrl += "?"; | |
191 } | |
192 | |
193 ret = WebRequest(method, outUrl + querystring, postData); | |
194 | |
195 return ret; | |
196 } | |
197 | |
198 /// <summary> | |
199 /// Web Request Wrapper | |
200 /// </summary> | |
201 /// <param name="method">Http Method</param> | |
202 /// <param name="url">Full url to the web resource</param> | |
203 /// <param name="postData">Data to post in querystring format</param> | |
204 /// <returns>The web server response.</returns> | |
205 public string WebRequest(Method method, string url, string postData) | |
206 { | |
207 HttpWebRequest webRequest = null; | |
208 StreamWriter requestWriter = null; | |
209 string responseData = ""; | |
210 | |
211 webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; | |
212 webRequest.Method = method.ToString(); | |
213 webRequest.ServicePoint.Expect100Continue = false; | |
214 //webRequest.UserAgent = "Identify your application please."; | |
215 //webRequest.Timeout = 20000; | |
216 | |
217 if (method == Method.POST || method == Method.DELETE) | |
218 { | |
219 webRequest.ContentType = "application/x-www-form-urlencoded"; | |
220 | |
221 //POST the data. | |
222 requestWriter = new StreamWriter(webRequest.GetRequestStream()); | |
223 try | |
224 { | |
225 requestWriter.Write(postData); | |
226 } | |
227 catch | |
228 { | |
229 throw; | |
230 } | |
231 finally | |
232 { | |
233 requestWriter.Close(); | |
234 requestWriter = null; | |
235 } | |
236 } | |
237 | |
238 responseData = WebResponseGet(webRequest); | |
239 | |
240 webRequest = null; | |
241 | |
242 return responseData; | |
243 | |
244 } | |
245 | |
246 /// <summary> | |
247 /// Process the web response. | |
248 /// </summary> | |
249 /// <param name="webRequest">The request object.</param> | |
250 /// <returns>The response data.</returns> | |
251 public string WebResponseGet(HttpWebRequest webRequest) | |
252 { | |
253 StreamReader responseReader = null; | |
254 string responseData = ""; | |
255 | |
256 try | |
257 { | |
258 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()); | |
259 responseData = responseReader.ReadToEnd(); | |
260 } | |
261 catch | |
262 { | |
263 throw; | |
264 } | |
265 finally | |
266 { | |
267 webRequest.GetResponse().GetResponseStream().Close(); | |
268 responseReader.Close(); | |
269 responseReader = null; | |
270 } | |
271 | |
272 return responseData; | |
273 } | |
274 } | |
275 } |