예제 #1
0
        private string GetOAuthToken(string consumerKey, string consumerSecret, Uri callback)
        {
            var oauthParameters = new OAuthParameterSet(consumerKey, consumerSecret)
            {
                { OAuthParameter.Callback, callback.ToString() },
            };

            string response;

            using (var webClient = new WebClient())
            {
                response = webClient.UploadString(GetRequestTokenUri(callback), string.Empty, oauthParameters);
            }

            if (response == null)
            {
                throw new InvalidOperationException("That didn't work then.");
            }

            Dictionary <string, string> values =
                response.Split('&').Select(section => section.Split('=')).ToDictionary(
                    bits => bits[0], bits => bits[1]);

            return(values["oauth_token"]);
        }
예제 #2
0
        public TwitterUser GetUser(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
        {
            var oauthParameters = new OAuthParameterSet(consumerKey, consumerSecret, oauthToken)
            {
                { OAuthParameter.Verifier, oauthVerifier }
            };
            string response;

            using (var webClient = new WebClient())
            {
                response = webClient.DownloadString(GetAccessTokenUri(), oauthParameters);
            }
            if (response == null)
            {
                throw new InvalidOperationException("That didn't work then.");
            }

            Dictionary <string, string> values =
                response.Split('&').Select(section => section.Split('=')).ToDictionary(
                    bits => bits[0], bits => bits[1]);

            return(new TwitterUser
            {
                Token = values["oauth_token"],
                TokenSecret = values["oauth_token_secret"],
                UserId = long.Parse(values["user_id"]),
                ScreenName = values["screen_name"]
            });
        }
예제 #3
0
// ReSharper disable InconsistentNaming
        public static void UploadStringAsync(this WebClient webClient, Uri uri, string body, OAuthParameterSet parameterSet)
// ReSharper restore InconsistentNaming
        {
            webClient.Headers.Set("Authorization", parameterSet.GetOAuthHeaderString(uri, "POST"));
            webClient.UploadStringAsync(uri, body);
        }
예제 #4
0
 public static string UploadString(this WebClient webClient, Uri uri, string body, OAuthParameterSet parameterSet)
 {
     webClient.Headers.Set("Authorization", parameterSet.GetOAuthHeaderString(uri, "POST"));
     return(webClient.UploadString(uri, body));
 }
예제 #5
0
 public static string DownloadString(this WebClient webClient, Uri uri, OAuthParameterSet parameterSet)
 {
     webClient.Headers.Set("Authorization", parameterSet.GetOAuthHeaderString(uri, "GET"));
     return(webClient.DownloadString(uri));
 }