Skip to content

Instantly share code, notes, and snippets.

@rzubek
Last active August 24, 2016 03:44
Show Gist options
  • Save rzubek/bf835d0bd18a4ba8295b072d5dd81a05 to your computer and use it in GitHub Desktop.
Save rzubek/bf835d0bd18a4ba8295b072d5dd81a05 to your computer and use it in GitHub Desktop.
/// <summary>
/// Uploads an image to Imgur
///
/// Credit: http://answers.unity3d.com/questions/1151550/how-can-i-upload-a-screenshot-directly-to-imgur.html
/// </summary>
public class ImgurScreenshotUploader {
/// <summary>
/// Blocking call that uploads an image and returns the results of the API call.
/// Results can be inspected to get the URL of the newly uploaded image, or the error code
/// if one occurred.
/// </summary>
/// <param name="rawImage">Raw bytes of a PNG or JPG file</param>
/// <returns>Returns the results of the API endpoint as a raw string</returns>
public static string Upload (byte[] rawImage) {
// Before we try uploading it to Imgur we need a Server Certificate Validation Callback
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
var values = new NameValueCollection() { { "image", Convert.ToBase64String(rawImage) }, { "type", "base64" } };
// Here's a version for using the commercial Imgur endpoint (Mashape.com)
using (var w = new WebClient()) {
w.Headers.Add("X-Mashape-Key", "XXXXXXXXXXXXXXXXX"); // <---- FILL IN YOUR MASHAPE KEY
w.Headers.Add("Authorization", "Client-ID XXXXXXX"); // <---- FILL IN YOUR CLIENT ID
byte[] response = w.UploadValues("https://imgur-apiv3.p.mashape.com/3/image", values);
string str = UTF8Encoding.UTF8.GetString(response);
Debug.Log(str);
return str;
}
//// And here's the version for uploading to the free Imgur API endpoint:
//
// using (var w = new WebClient()) {
// w.Headers.Add("Authorization", "Client-ID XXXXXXX"); // <---- FILL IN YOUR CLIENT ID
// byte[] response = w.UploadValues("https://api.imgur.com/3/image.xml", values);
// Debug.Log(XDocument.Load(new MemoryStream(response)));
// return UTF8Encoding.UTF8.GetString(response);
// }
}
private static bool MyRemoteCertificateValidationCallback (System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
bool isOk = true;
// If there are errors in the certificate chain, look at each error to determine the cause.
if (sslPolicyErrors != SslPolicyErrors.None) {
for (int i = 0; i < chain.ChainStatus.Length; i++) {
if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown) {
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build((X509Certificate2)certificate);
if (!chainIsValid) {
isOk = false;
}
}
}
}
return isOk;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment