Exemple #1
0
        /// <summary>
        /// Gets a new OAuth request token from the twitter api.
        /// </summary>
        /// <param name="consumerKey">The consumer key.</param>
        /// <param name="consumerSecret">The consumer secret.</param>
        /// <param name="callbackAddress">Address of the callback.</param>
        /// <returns>
        /// A new <see cref="Twitterizer.OAuthTokenResponse"/> instance.
        /// </returns>
        public static OAuthTokenResponse GetRequestToken(string consumerKey, string consumerSecret, string callbackAddress, WebProxy proxy)
        {
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(consumerSecret))
            {
                throw new ArgumentNullException("consumerSecret");
            }

            OAuthTokenResponse response = new OAuthTokenResponse();

            Dictionary<string, string> parameters = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(callbackAddress))
            {
                parameters.Add("oauth_callback", callbackAddress);
            }

            try
            {
                HttpWebResponse webResponse = ExecuteRequest(
                    "https://api.twitter.com/oauth/request_token",
                    parameters,
                    HTTPVerb.POST,
                    consumerKey,
                    consumerSecret,
                    null,
                    null,
                    proxy);

                string responseBody = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();

                Match matchedValues = Regex.Match(responseBody, @"oauth_token=(?<token>[^&]+)|oauth_token_secret=(?<secret>[^&]+)|oauth_verifier=(?<verifier>[^&]+)");

                response.Token = matchedValues.Groups["token"].Value;
                response.TokenSecret = matchedValues.Groups["secret"].Value;
                response.VerificationString = matchedValues.Groups["verifier"].Value;
            }
            catch (WebException wex)
            {
                throw new Exception(wex.Message, wex);
            }

            return response;
        }
Exemple #2
0
        /// <summary>
        /// Gets the access token from pin.
        /// </summary>
        /// <param name="consumerKey">The consumer key.</param>
        /// <param name="consumerSecret">The consumer secret.</param>
        /// <param name="requestToken">The request token.</param>
        /// <param name="verifier">The pin number or verifier string.</param>
        /// <param name="proxy">The proxy.</param>
        /// <returns>
        /// An <see cref="OAuthTokenResponse"/> class containing access token information.
        /// </returns>
        public static OAuthTokenResponse GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string verifier, WebProxy proxy)
        {
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(consumerSecret))
            {
                throw new ArgumentNullException("consumerSecret");
            }

            if (string.IsNullOrEmpty(requestToken))
            {
                throw new ArgumentNullException("requestToken");
            }

            OAuthTokenResponse response = new OAuthTokenResponse();

            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();

                if (!string.IsNullOrEmpty(verifier))
                {
                    parameters.Add("oauth_verifier", verifier);
                }

                HttpWebResponse webResponse = ExecuteRequest(
                    "https://api.twitter.com/oauth/access_token",
                    parameters,
                    HTTPVerb.POST,
                    consumerKey,
                    consumerSecret,
                    requestToken,
                    string.Empty,
                    proxy);

                string responseBody = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();

                response.Token = Regex.Match(responseBody, @"oauth_token=([^&]+)").Groups[1].Value;
                response.TokenSecret = Regex.Match(responseBody, @"oauth_token_secret=([^&]+)").Groups[1].Value;
                response.UserId = long.Parse(Regex.Match(responseBody, @"user_id=([^&]+)").Groups[1].Value, CultureInfo.CurrentCulture);
                response.ScreenName = Regex.Match(responseBody, @"screen_name=([^&]+)").Groups[1].Value;
            }
            catch (WebException wex)
            {
                throw new Exception(wex.Message, wex);
            }

            return response;
        }