public string SendGist(string fileName, string content, string description, bool isPublic, GitHubCredentials credentials) { var gistAsJson = new CreatesGistMessages().CreateMessage(fileName, content, description, isPublic); using (var stream = new MemoryStream()) { var request = new FluentHttpRequest() .BaseUrl("https://api.github.com") .ResourcePath("/gists") .Method("POST") .Headers(h => h.Add("User-Agent", "Gister")) .Headers(h => h.Add("Content-Type", "application/json")) .Body(x => x.Append(gistAsJson)) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(stream)); AppliesGitHubCredentialsToFluentHttpRequest.ApplyCredentials(credentials, request); var response = request .Execute(); if (response.Response.HttpWebResponse.StatusCode != HttpStatusCode.Created) throw new GitHubUnauthorizedException(response.Response.HttpWebResponse.StatusDescription); return PeelOutGistHtmlUrl(response); } }
private static void Delete(string postId) { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath(postId) .Method("DELETE") .QueryStrings(qs => qs.Add("access_token", AccessToken)) .Headers(h => h.Add("User-Agent", "FluentHttp")) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); // Execute the request. Call EndRequest immediately so it behaves synchronously. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); // Print the response Console.WriteLine("Delete: "); Console.WriteLine(FluentHttpRequest.ToString(responseSaveStream)); }
private static void Get() { //Stream to save the response to var responseSaveStream = new MemoryStream(); //Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/4") .Method("GET") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(q => q .Add("fields", "name,first_name,last_name") .Add("format", "json")) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); var asyncResult = request.Execute(); //seek the save stream to beginning. asyncResult.Response.SaveStream.Seek(0, SeekOrigin.Begin); //Print the response Console.WriteLine("Get: "); Console.WriteLine(FluentHttpRequest.ToString(asyncResult.Response.SaveStream)); }
public string SendGist(string fileName, string content, string description, bool isPublic, GitHubCredentials credentials) { var gistAsJson = new CreatesGistMessages().CreateMessage(fileName, content, description, isPublic); using (var stream = new MemoryStream()) { var request = new FluentHttpRequest() .BaseUrl("https://api.github.com") .ResourcePath("/gists") .Method("POST") .Headers(h => h.Add("User-Agent", "Gister")) .Headers(h => h.Add("Content-Type", "application/json")) .Body(x => x.Append(gistAsJson)) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(stream)); AppliesGitHubCredentialsToFluentHttpRequest.ApplyCredentials(credentials, request); var response = request .Execute(); if (response.Response.HttpWebResponse.StatusCode != HttpStatusCode.Created) { throw new GitHubUnauthorizedException(response.Response.HttpWebResponse.StatusDescription); } return(PeelOutGistHtmlUrl(response)); } }
public static string UploadPhoto(string path, string filename, string contentType, string message) { var parameters = new Dictionary <string, object>(); parameters["message"] = message; parameters["file1"] = new MediaObject { ContentType = contentType, FileName = Path.GetFileName(filename) } .SetValue(File.ReadAllBytes(path)); // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/photos") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(qs => qs.Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => AttachRequestBodyAndUpdateHeader(body.Request, parameters, null)); // Execute the request. Call EndRequest immediately so it behaves synchronously. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); if (ar.Exception != null) { throw ar.Exception; } // Print the response Console.WriteLine("Upload photo: "); Console.WriteLine(responseResult); if (ar.InnerException != null) { throw ar.InnerException; } return((string)json["id"]); }
public static string UploadPhoto(string path, string filename, string contentType, string message) { var parameters = new Dictionary<string, object>(); parameters["message"] = message; parameters["file1"] = new MediaObject { ContentType = contentType, FileName = Path.GetFileName(filename) } .SetValue(File.ReadAllBytes(path)); // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/photos") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(qs => qs.Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => AttachRequestBodyAndUpdateHeader(body.Request, parameters, null)); // Execute the request. Call EndRequest immediately so it behaves synchronously. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary<string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); if (ar.Exception != null) { throw ar.Exception; } // Print the response Console.WriteLine("Upload photo: "); Console.WriteLine(responseResult); if (ar.InnerException != null) { throw ar.InnerException; } return (string)json["id"]; }
private static string Post(string message) { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/feed") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(q => q .Add("format", "json") .Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => { var parameters = new Dictionary <string, object>(); parameters["message"] = message; AttachRequestBodyAndUpdateHeader(body.Request, parameters, null); }); // Execute the request. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); // Print the response Console.WriteLine("Post: "); Console.WriteLine(responseResult); return((string)json["id"]); }
private string GetFacebookToken() { var oauthuri = Consts.BASE_URI + "/oauth/access_token?client_id=******&client_secret=****&grant_type=client_credentials"; using (var responseSaveStream = new MemoryStream()) { var request = new FluentHttpRequest() .BaseUrl(oauthuri) .Method("GET") .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); var ar = request.Execute(); var response = ar.Response; // seek the save stream to beginning. response.SaveStream.Seek(0, SeekOrigin.Begin); var token = FluentHttpRequest.ToString(response.SaveStream); return token.Split('=')[1]; } }
private static string Post(string message) { // Stream to save the response to var responseSaveStream = new MemoryStream(); // Prepare the request. var request = new FluentHttpRequest() .BaseUrl("https://graph.facebook.com") .ResourcePath("/me/feed") .Method("POST") .Headers(h => h.Add("User-Agent", "FluentHttp")) .QueryStrings(q => q .Add("format", "json") .Add("access_token", AccessToken)) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)) .Body(body => { var parameters = new Dictionary<string, object>(); parameters["message"] = message; AttachRequestBodyAndUpdateHeader(body.Request, parameters, null); }); // Execute the request. var ar = request.Execute(); // seek the save stream to beginning. responseSaveStream.Seek(0, SeekOrigin.Begin); var responseResult = FluentHttpRequest.ToString(responseSaveStream); // Convert to json var json = (IDictionary<string, object>)SimpleJson.SimpleJson.DeserializeObject(responseResult); // Print the response Console.WriteLine("Post: "); Console.WriteLine(responseResult); return (string)json["id"]; }
private void GetAllResultsRecursive(string uri, IDictionary<string, object> query, List<string> results, IDictionary<string, object> projectparameters) { if (!IsStarted) return; string json = string.Empty; try { using (var responseSaveStream = new MemoryStream()) { var request = new FluentHttpRequest() .BaseUrl(uri) .Method("GET") .QueryStrings(q => q .Add(query) //add all query strings ) .Proxy(WebRequest.DefaultWebProxy) .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseSaveStream)); request.AuthenticateUsing(new OAuth2AuthorizationRequestHeaderBearerAuthenticator(GetFacebookToken())); var ar = request.Execute(); var response = ar.Response; // seek the save stream to beginning. response.SaveStream.Seek(0, SeekOrigin.Begin); json = FluentHttpRequest.ToString(response.SaveStream); Dictionary<string, object> responseobject = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); if (responseobject.ContainsKey(Consts.DATA_KEY) && responseobject[Consts.DATA_KEY].HasValue()) { IList<object> datas = JsonConvert.DeserializeObject<IList<object>>(responseobject[Consts.DATA_KEY].ToString()); if (datas.Count > 0) { for (int i = 0; i < datas.Count; i++) { IDictionary<string, object> resultdic = JsonConvert.DeserializeObject<Dictionary<string, object>>(datas[i].ToString()); //resultdic.Add(Consts.WORDS_KEY, projectparameters); //resultdic.Add(Consts.PROVIDER_KEY, "FaceBook"); if (resultdic.ContainsKey(Consts.DESCRIPTION)) { var message = HttpUtility.HtmlDecode(resultdic[Consts.DESCRIPTION].ToString()); results.Add(message.Substring(0, message.Length < 140 ? message.Length : 140)); } //else if (resultdic.ContainsKey(Consts.MESSAGE)) //{ // var message = HttpUtility.HtmlDecode(resultdic[Consts.MESSAGE].ToString()); // results.Add(message.Substring(0, message.Length < 140 ? message.Length : 140)); // } //results.Add(JsonConvert.SerializeObject(resultdic)); resultdic = null; } foreach (var post in results) { if (!IsStarted) return; string p = string.Empty; if (!string.IsNullOrEmpty(additionalString)) { if (140 - post.Length > additionalString.Length + 1) p = post + " " + additionalString; else p = post.Substring(0, post.Length - additionalString.Length + 1) + " " + additionalString; } _twitterService.SendTweet(new SendTweetOptions { Status = p }); _form.Log(post); System.Threading.Thread.Sleep(2000);//twitter kızmasın } if (responseobject.ContainsKey(Consts.PAGING_KEY) && responseobject[Consts.PAGING_KEY].HasValue()) { IDictionary<string, string> nextprevuri = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseobject[Consts.PAGING_KEY].ToString()); if (nextprevuri.ContainsKey(Consts.NEXT_PAGE_KEY) && nextprevuri[Consts.NEXT_PAGE_KEY].HasValue()) { Uri urim = new Uri(nextprevuri[Consts.NEXT_PAGE_KEY].ToString()); var querystrings = ToDictionary(HttpUtility.ParseQueryString(urim.Query)); urim = null; GetAllResultsRecursive(m_baseUrl, querystrings, results, projectparameters); if (string.IsNullOrEmpty(m_untilValue) && querystrings[Consts.UNTIL_KEY].HasValue()) { m_untilValue = querystrings[Consts.UNTIL_KEY].ToString(); WriteSince(querystrings[Consts.QUERY_KEY].ToString(), m_untilValue); } } } } } request = null; GC.Collect(); } } catch (Exception ex) { throw; } }