コード例 #1
0
        public static ImageInfo toImgur(Bitmap bmp)
        {
            ImageConverter convert = new ImageConverter();

            byte[] toSend = (byte[])convert.ConvertTo(bmp, typeof(byte[]));
            using (WebClient wc = new WebClient())
            {
                NameValueCollection nvc = new NameValueCollection
                {
                    { "image", Convert.ToBase64String(toSend) }
                };
                wc.Headers.Add("Authorization", Imgur.getAuth());
                ImageInfo info = new ImageInfo();
                try
                {
                    byte[] response = wc.UploadValues("https://api.imgur.com/3/upload.xml", nvc);
                    string res      = System.Text.Encoding.Default.GetString(response);

                    var xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.LoadXml(res);
                    info.link       = new Uri(xmlDoc.SelectSingleNode("data/link").InnerText);
                    info.deletehash = xmlDoc.SelectSingleNode("data/deletehash").InnerText;
                    info.id         = xmlDoc.SelectSingleNode("data/id").InnerText;
                    info.success    = true;
                }
                catch (Exception e)
                {
                    info.success = false;
                    info.ex      = e;
                }
                return(info);
            }
        }
コード例 #2
0
 public static void deleteImage(ImageInfo info)
 {
     using (WebClient wc = new WebClient())
     {
         wc.Headers.Add("Authorization", Imgur.getAuth());
         byte[] response = wc.UploadValues(String.Format("https://api.imgur.com/3/image/{0}", info.deletehash), "DELETE", new NameValueCollection());
     }
 }
コード例 #3
0
 public static AuthInfo authAccount(string pin)
 {
     using (WebClient wc = new WebClient())
     {
         NameValueCollection nvc = new NameValueCollection
         {
             { "client_id", APIKeys.ImgurClientID },
             { "client_secret", APIKeys.ImgurClientSecret },
             { "grant_type", "pin" },
             { "pin", pin },
         };
         wc.Headers.Add("Authorization", Imgur.getAuth());
         AuthInfo info = new AuthInfo();
         try
         {
             byte[] response   = wc.UploadValues("https://api.imgur.com/oauth2/token", nvc);
             string res        = System.Text.Encoding.Default.GetString(response);
             var    JsonReader = JsonReaderWriterFactory.CreateJsonReader(response, new System.Xml.XmlDictionaryReaderQuotas());
             var    root       = System.Xml.Linq.XElement.Load(JsonReader);
             info.access_token     = root.Element("access_token").Value;
             info.expires_in       = root.Element("expires_in").Value;
             info.token_type       = root.Element("token_type").Value;
             info.scope            = root.Element("scope").Value;
             info.refresh_token    = root.Element("refresh_token").Value;
             info.account_id       = Int32.Parse(root.Element("account_id").Value);
             info.account_username = root.Element("account_username").Value;
             info.success          = true;
         }
         catch (Exception e)
         {
             info.success = false;
             info.ex      = e;
         }
         return(info);
     }
 }