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