Example #1
0
        public AuthResponse Authenticate(AuthenticateSettings authenticateSettings)
        {
            AuthResponse twitAuthResponse = null;
            // Do the Authenticate
            var authHeaderFormat = "Basic {0}";

            var authHeader = string.Format(
                authHeaderFormat,
                Convert.ToBase64String(
                    Encoding.UTF8.GetBytes(
                        Uri.EscapeDataString(authenticateSettings.OAuthConsumerKey) +
                        ":" +
                        Uri.EscapeDataString(authenticateSettings.OAuthConsumerSecret)
                    )
                )
            );

            var postBody = "grant_type=client_credentials";
            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(authenticateSettings.OAuthUrl);

            authRequest.Headers.Add("Authorization", authHeader);
            authRequest.Method = "POST";
            authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (Stream stream = authRequest.GetRequestStream()) {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }

            authRequest.Headers.Add("Accept-Encoding", "gzip");

            WebResponse authResponse = authRequest.GetResponse();

            // deserialize into an object
            using (authResponse) {
                using (var reader = new StreamReader(authResponse.GetResponseStream())) {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objectText = reader.ReadToEnd();
                    twitAuthResponse = JsonConvert.DeserializeObject<AuthResponse>(objectText);
                }
            }

            return twitAuthResponse;
        }
        public static OAuthTwitterHelper GetTwitterHelper()
        {
            if (_twitterHelper == null) {
                string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
                string oAuth2TokenUrl = ConfigurationManager.AppSettings["twitterOAuth2TokenUrl"];

                var authSettings = new AuthenticateSettings {
                    OAuthConsumerKey = consumerKey,
                    OAuthConsumerSecret = consumerSecret,
                    OAuthUrl = oAuth2TokenUrl
                };

                _twitterHelper = new OAuthTwitterHelper(authSettings);
            }

            return _twitterHelper;
        }
 public OAuthTwitterHelper(AuthenticateSettings authSettings)
 {
     _authSettings = authSettings;
 }