public string RequstJson(string apiUrl, string tokenType, string accessToken) { var json = string.Empty; HttpWebRequest apiRequest = (HttpWebRequest)WebRequest.Create(apiUrl); const string timelineHeaderFormat = "{0} {1}"; apiRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, tokenType, accessToken)); apiRequest.Method = "Get"; try { WebResponse timeLineResponse = apiRequest.GetResponse(); using (timeLineResponse) { using (var reader = new StreamReader(timeLineResponse.GetResponseStream())) { json = reader.ReadToEnd(); } } } catch (WebException e) { //Console.Write("Utility.RequstJson : WebException = " + e.Status); EventLogWriter logWriter = new EventLogWriter("oAuthTwitterWrapper"); logWriter.WriteErrorToEventLog("Utility.RequstJson : WebException = " + e.Status); json = string.Format("Error: {0}", e.Message); } return(json); }
public TwitAuthenticateResponse AuthenticateMe(string oAuthConsumerKey, string oAuthConsumerSecret, string oAuthUrl) { //oAuthConsumerKey = "sLE6zXtRITflCiPIASkg"; //oAuthConsumerSecret = "CZD1Pl6sRfALb2m4SFViMsbC9Hl8Lz38CGumDwgYwEM"; EventLogWriter logWriter = new EventLogWriter("oAuthTwitterWrapper"); TwitAuthenticateResponse twitAuthResponse; // Do the Authenticate const string authHeaderFormat = "Basic {0}"; var authHeader = string.Format(authHeaderFormat, Convert.ToBase64String( Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" + Uri.EscapeDataString((oAuthConsumerSecret))) )); const string postBody = "grant_type=client_credentials"; HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(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 = Encoding.ASCII.GetBytes(postBody); stream.Write(content, 0, content.Length); } authRequest.Headers.Add("Accept-Encoding", "gzip"); try { 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 <TwitAuthenticateResponse>(objectText); } } return(twitAuthResponse); } catch (WebException e) { logWriter.WriteErrorToEventLog(string.Format("Authenticate.TwitAuthenticateResponse: {0}", e.Message)); } return(new TwitAuthenticateResponse()); }
public static void Connect(string initialCatalog) { if (_connection != null) { if (_connection.State == ConnectionState.Open) { return; } } if (_connection != null && _connection.State == ConnectionState.Connecting) { return; } lock (new object()) { _connection = new SqlConnection { ConnectionString = ConnectionString }; if (_connection.State != ConnectionState.Open) { try { _connection.Open(); _isConnected = true; } catch (Exception e) { logWriter.WriteErrorToEventLog(string.Format("DataHelper.Connect: {0}", e.Message)); if (_connection.State != ConnectionState.Open) { _isConnected = false; } } } } }
public TwitterItem(XElement tweet) { EventLogWriter logWriter = new EventLogWriter("TwitterManager"); //Message Id try { MessageId = decimal.Parse(tweet.Element("id").Value); } catch (Exception ex) { logWriter.WriteErrorToEventLog(string.Format("TwitterItem: Exception = {0}", ex.Message)); } //Created At try { string date = tweet.Element("created_at").Value; CreatedAt = DateTime.Parse(date.Substring(date.Length - 4, 4) + date.Substring(4, 19)); } catch (Exception ex) { logWriter.WriteErrorToEventLog(string.Format("TwitterItem: Exception = {0}", ex.Message)); } //User Id try { UserId = int.Parse(tweet.Element("user").Element("id").Value); } catch (Exception ex) { logWriter.WriteErrorToEventLog(string.Format("TwitterItem: Exception = {0}", ex.Message)); } //Message Message = tweet.Element("text").Value; //Screen Name ScreenName = tweet.Element("user").Element("screen_name").Value; //Language Language = tweet.Element("user").Element("lang").Value; //InReplyToStatusId try { InReplyToStatusId = tweet.Element("in_reply_to_status_id").Value == "" ?0 :decimal.Parse(tweet.Element("in_reply_to_status_id").Value); } catch (Exception ex) { logWriter.WriteErrorToEventLog(string.Format("TwitterItem: Exception = {0}", ex.Message)); } //InReplyToUserId try { InReplyToUserId = tweet.Element("in_reply_to_user_id").Value == "" ?0 :int.Parse(tweet.Element("in_reply_to_user_id").Value); } catch (Exception ex) { logWriter.WriteErrorToEventLog(string.Format("TwitterItem: Exception = {0}", ex.Message)); } //InReplyToScreenName InReplyToScreenName = tweet.Element("in_reply_to_screen_name").Value; UploadedBy = Environment.MachineName; UploadedAt = DateTime.Now; }