diff 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
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs	Sat Jun 04 00:07:20 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs	Sat Jun 04 12:11:17 2011 -0300
@@ -7,7 +7,7 @@
 
 namespace AltNetHispano.Agendas.Twitter
 {
-    public class oAuthTwitter : OAuthBase
+    public class OAuthTwitter : OAuthBase
     {
         public enum Method { GET, POST, DELETE };
         public const string REQUEST_TOKEN = "http://twitter.com/oauth/request_token";
@@ -21,9 +21,7 @@
         private string _callBackUrl = "oob";
         private string _oauthVerifier = "";
 
-
-#region Properties
-        public string ConsumerKey 
+    	private string ConsumerKey 
         {
             get
             {
@@ -32,11 +30,10 @@
                     _consumerKey = ConfigurationManager.AppSettings["consumerKey"];
                 }
                 return _consumerKey; 
-            } 
-            set { _consumerKey = value; } 
+            }
         }
-        
-        public string ConsumerSecret { 
+
+    	private string ConsumerSecret { 
             get {
                 if (_consumerSecret.Length == 0)
                 {
@@ -44,7 +41,6 @@
                 }
                 return _consumerSecret; 
             } 
-            set { _consumerSecret = value; } 
         }
 
         public string Token { get { return _token; } set { _token = value; } }
@@ -52,8 +48,6 @@
         public string CallBackUrl { get { return _callBackUrl; } set { _callBackUrl = value; } }
         public string OAuthVerifier { get { return _oauthVerifier; } set { _oauthVerifier = value; } }
 
-#endregion
-
         /// <summary>
         /// Get the link to Twitter's authorization page for this application.
         /// </summary>
@@ -62,7 +56,7 @@
         {
             string ret = null;
 
-            string response = oAuthWebRequest(Method.GET, REQUEST_TOKEN, String.Empty);
+            string response = OAuthWebRequest(Method.GET, REQUEST_TOKEN, String.Empty);
             if (response.Length > 0)
             {
                 //response contains token and token secret.  We only need the token.
@@ -76,9 +70,9 @@
                     }
                 }
 
-                if (qs["oauth_token"] != null)
+                if (qs[OAuthTokenKey] != null)
                 {
-                    ret = AUTHORIZE + "?oauth_token=" + qs["oauth_token"];
+					ret = AUTHORIZE + "?" + OAuthTokenKey + "=" + qs[OAuthTokenKey];
                 }
             }
             return ret;
@@ -91,22 +85,22 @@
         /// <param name="oauthVerifier">An oauth_verifier parameter is provided to the client either in the pre-configured callback URL</param>
         public void AccessTokenGet(string authToken, string oauthVerifier)
         {
-            this.Token = authToken;
-            this.OAuthVerifier = oauthVerifier;
+            Token = authToken;
+            OAuthVerifier = oauthVerifier;
 
-            string response = oAuthWebRequest(Method.GET, ACCESS_TOKEN, String.Empty);
+            string response = OAuthWebRequest(Method.GET, ACCESS_TOKEN, String.Empty);
 
             if (response.Length > 0)
             {
                 //Store the Token and Token Secret
                 NameValueCollection qs = HttpUtility.ParseQueryString(response);
-                if (qs["oauth_token"] != null)
+                if (qs[OAuthTokenKey] != null)
                 {
-                    this.Token = qs["oauth_token"];
+					Token = qs[OAuthTokenKey];
                 }
-                if (qs["oauth_token_secret"] != null)
+				if (qs[OAuthTokenSecretKey] != null)
                 {
-                    this.TokenSecret = qs["oauth_token_secret"];
+					TokenSecret = qs[OAuthTokenSecretKey];
                 }
             }
         }
@@ -118,11 +112,11 @@
         /// <param name="url">The full url, including the querystring.</param>
         /// <param name="postData">Data to post (querystring format)</param>
         /// <returns>The web server response.</returns>
-        public string oAuthWebRequest(Method method, string url, string postData)
+        public string OAuthWebRequest(Method method, string url, string postData)
         {
-            string outUrl = "";
-            string querystring = "";
-            string ret = "";
+            string outUrl;
+            string querystring;
+            string ret;
 
 
             //Setup postData for signing.
@@ -141,7 +135,7 @@
                             postData += "&";
                         }
                         qs[key] = HttpUtility.UrlDecode(qs[key]);
-                        qs[key] = this.UrlEncode(qs[key]);
+                        qs[key] = UrlEncode(qs[key]);
                         postData += key + "=" + qs[key];
 
                     }
@@ -157,26 +151,26 @@
                 }
             }
 
-            Uri uri = new Uri(url);
+            var uri = new Uri(url);
             
-            string nonce = this.GenerateNonce();
-            string timeStamp = this.GenerateTimeStamp();
+            string nonce = GenerateNonce();
+            string timeStamp = GenerateTimeStamp();
 
             //Generate Signature
-            string sig = this.GenerateSignature(uri,
-                this.ConsumerKey,
-                this.ConsumerSecret,
-                this.Token,
-                this.TokenSecret,
-                this.CallBackUrl,
-                this.OAuthVerifier,
+            string sig = GenerateSignature(uri,
+                ConsumerKey,
+                ConsumerSecret,
+                Token,
+                TokenSecret,
+                CallBackUrl,
+                OAuthVerifier,
                 method.ToString(),
                 timeStamp,
                 nonce,
                 out outUrl,
                 out querystring);
 
-            querystring += "&oauth_signature=" + this.UrlEncode(sig);
+        	querystring += "&" + OAuthSignatureKey + "=" + UrlEncode(sig);
 
             //Convert the querystring to postData
             if (method == Method.POST || method == Method.DELETE)
@@ -204,9 +198,9 @@
         /// <returns>The web server response.</returns>
         public string WebRequest(Method method, string url, string postData)
         {
-            HttpWebRequest webRequest = null;
-            StreamWriter requestWriter = null;
-            string responseData = "";
+            HttpWebRequest webRequest;
+            StreamWriter requestWriter;
+            string responseData;
 
             webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
             webRequest.Method = method.ToString();
@@ -224,22 +218,15 @@
                 {
                     requestWriter.Write(postData);
                 }
-                catch
-                {
-                    throw;
-                }
                 finally
                 {
                     requestWriter.Close();
-                    requestWriter = null;
                 }
             }
 
             responseData = WebResponseGet(webRequest);
 
-            webRequest = null;
-
-            return responseData;
+        	return responseData;
 
         }
 
@@ -251,22 +238,17 @@
         public string WebResponseGet(HttpWebRequest webRequest)
         {
             StreamReader responseReader = null;
-            string responseData = "";
+            string responseData;
 
             try
             {
                 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                 responseData = responseReader.ReadToEnd();
             }
-            catch
-            {
-                throw;
-            }
             finally
             {
                 webRequest.GetResponse().GetResponseStream().Close();
                 responseReader.Close();
-                responseReader = null;
             }
 
             return responseData;