/// <summary> /// Gets the Twitter feed for the user based on the configuration settings /// </summary> /// <returns></returns> public string GetTwitterFeed() { //Reference : https://dev.twitter.com/rest/reference/get/statuses/user_timeline // Get Configuration details needed for Authentication Token. string oAuthConsumerKey = ConfigurationManager.AppSettings["oAuthConsumerKey"]; string oAuthConsumerSecret = ConfigurationManager.AppSettings["oAuthConsumerSecret"]; string oAuthUrl = ConfigurationManager.AppSettings["oAuthUrl"]; //Get the Configuration settings needed for twitter timeline feed string timelineFormat = ConfigurationManager.AppSettings["timelineFormat"]; string screenname = ConfigurationManager.AppSettings["screenname"]; int count = Convert.ToInt16(ConfigurationManager.AppSettings["count"]); string include_rts = ConfigurationManager.AppSettings["include_rts"]; string exclude_replies = ConfigurationManager.AppSettings["exclude_replies"]; // Return variable string timeLineJson = string.Empty; // Get Authentication token details AuthenticateSettings = new AuthenticateSettings { OAuthConsumerKey = oAuthConsumerKey, OAuthConsumerSecret = oAuthConsumerSecret, OAuthUrl = oAuthUrl }; IAuthenticate authenticate = new Authenticate(); AuthResponse twitAuthResponse = authenticate.AuthenticateConsumer(AuthenticateSettings); //Create a URL to get twitter feed with appropriate Parameter. string TimelineUrl = string.Format(timelineFormat, screenname, include_rts, exclude_replies, count); // Get Twitter feed in Json format. if (twitAuthResponse != null) { timeLineJson = GetTwitterFeedJson(TimelineUrl, twitAuthResponse.TokenType, twitAuthResponse.AccessToken); } return(timeLineJson); }
/// <summary> /// Provides authentication application token based on consumer key information. /// Returns null if any of the input parameter does not contains the values. /// /// </summary> /// <param name="authenticateSettings"></param> /// <returns></returns> //Reference: "Issuing application-only requests" https://dev.twitter.com/oauth/application-only public AuthResponse AuthenticateConsumer(AuthenticateSettings authenticateSettings) { try { // Validate the Parameter value. Parameter should have value, otherwise return null. // Also all 3 properties should have values, otherwise return null. if (authenticateSettings == null) { return(null); } else if (authenticateSettings.OAuthConsumerKey == null || authenticateSettings.OAuthConsumerSecret == null || authenticateSettings.OAuthUrl == null) { return(null); } else if (authenticateSettings.OAuthConsumerKey == string.Empty || authenticateSettings.OAuthConsumerSecret == string.Empty || authenticateSettings.OAuthUrl == string.Empty) { return(null); } AuthResponse twitAuthResponse = new AuthResponse(); //Prepare the Authentication header var authHeaderFormat = "Basic {0}"; var authHeader = string.Format(authHeaderFormat, Convert.ToBase64String( Encoding.UTF8.GetBytes(Uri.EscapeDataString(authenticateSettings.OAuthConsumerKey) + ":" + Uri.EscapeDataString((authenticateSettings.OAuthConsumerSecret))) )); //HTTP Request object HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(authenticateSettings.OAuthUrl); //Add header information as per detail provided in twitter api authRequest.Headers.Add("Authorization", authHeader); authRequest.Method = "POST"; authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; //Write below grant type details to Request var postBody = "grant_type=client_credentials"; 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(); // read the response and return as AuthResponse object using (authResponse) { using (var reader = new StreamReader(authResponse.GetResponseStream())) { string objectText = reader.ReadToEnd(); if (objectText != "" || objectText != null) { //Convert the token detils in Jason format to AuthResponse object JavaScriptSerializer json_serializer = new JavaScriptSerializer(); var authToken = json_serializer.Deserialize <Dictionary <string, string> >(objectText); if (authToken != null) { twitAuthResponse.TokenType = authToken["token_type"]; twitAuthResponse.AccessToken = authToken["access_token"]; } } } } return(twitAuthResponse); } catch (Exception ex) { //Handle specific action like Logging if needed. Then throw the original exception throw ex; } }