Last active
December 17, 2015 08:09
-
-
Save ryanvalentin/5578268 to your computer and use it in GitHub Desktop.
Disqus API methods for guest commenting Windows Phone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json.Linq; | |
using System.Net.NetworkInformation; | |
using System.Net.Http; | |
using AdvancedREI.Net.Http.Compression; | |
using example_project.Resources; | |
namespace example_project | |
{ | |
public static class ExternalApi | |
{ | |
public static async Task<string> GetCommentData(string endpoint, string arguments, string AccessToken) | |
{ | |
if (!NetworkInterface.GetIsNetworkAvailable()) | |
{ | |
return "Error: Not connected to the internet"; | |
} | |
// Create HTTP client | |
var gziphandler = new CompressedHttpClientHandler(); | |
var httpClient = new HttpClient(gziphandler); | |
httpClient.DefaultRequestHeaders.Referrer = new Uri("http://example.com/", UriKind.Absolute); | |
httpClient.DefaultRequestHeaders.Host = "example.com"; | |
var response = await httpClient.GetAsync(endpoint + "?api_key=" + ApiResources.key_public + "&api_secret=" + ApiResources.key_secret + AccessToken + arguments); | |
// Return content as a string | |
string content = await response.Content.ReadAsStringAsync(); | |
try | |
{ | |
response.EnsureSuccessStatusCode(); | |
return content; | |
} | |
catch (HttpRequestException) | |
{ | |
try | |
{ | |
JObject resultObject = JObject.Parse(content); | |
return "Error: " + (string)resultObject["response"]; | |
} | |
catch | |
{ | |
return "Error: " + "Unable to connect to the Disqus servers. Check your ineternet connection and try again"; | |
} | |
} | |
catch | |
{ | |
return "Error: " + "Unable to connect to the Disqus servers. Check your ineternet connection and try again"; | |
} | |
} | |
/// <summary> | |
/// Method to post a new comment as a guest | |
/// </summary> | |
public static async Task<string> PostComment(string threadid, string message, string guestemail, string guestname, bool authenticated, string access_token) | |
{ | |
if (!NetworkInterface.GetIsNetworkAvailable()) | |
{ | |
return "Error: Not connected to the internet"; | |
} | |
// Create HTTP client | |
var gziphandler = new CompressedHttpClientHandler(); | |
var httpClient = new HttpClient(gziphandler); | |
httpClient.DefaultRequestHeaders.Referrer = new Uri("http://example.com/", UriKind.Absolute); | |
httpClient.DefaultRequestHeaders.Host = "example.com"; | |
// This is the postdata | |
var postData = new List<KeyValuePair<string, string>>(); | |
postData.Add(new KeyValuePair<string, string>("api_key", ApiResources.key_public)); | |
postData.Add(new KeyValuePair<string, string>("api_secret", ApiResources.key_secret)); | |
postData.Add(new KeyValuePair<string, string>("thread", threadid)); | |
postData.Add(new KeyValuePair<string, string>("message", message)); | |
postData.Add(new KeyValuePair<string, string>("strict", "1")); // Set strict so optional params aren't dropped when there's an exception | |
if (authenticated == true) | |
{ | |
postData.Add(new KeyValuePair<string, string>("access_token", access_token)); | |
} | |
else | |
{ | |
postData.Add(new KeyValuePair<string, string>("author_email", guestemail)); | |
postData.Add(new KeyValuePair<string, string>("author_name", guestname)); | |
} | |
var response = await httpClient.PostAsync(new Uri(ApiResources.endpoint_posts_create, UriKind.Absolute), new FormUrlEncodedContent(postData)); | |
// Return content as a string | |
string content = await response.Content.ReadAsStringAsync(); | |
try | |
{ | |
response.EnsureSuccessStatusCode(); | |
return content; | |
} | |
catch (HttpRequestException) | |
{ | |
try | |
{ | |
JObject resultObject = JObject.Parse(content); | |
// Get code | |
//string responsecode = (string)resultObject["code"]; | |
// TODO check the error code against this page to give a more detailed response:http://disqus.com/api/docs/errors/ | |
// If there's a permission error, that usually means the user/IP address has been blacklisted | |
return "Error: " + (string)resultObject["response"]; | |
} | |
catch | |
{ | |
return "Error: Unable to post this comment"; | |
} | |
} | |
catch | |
{ | |
return "Error: Unable to post this comment"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out our API documentation to find out what endpoints are available and what their response types are: http://disqus.com/api/docs/