Uploading to imgur.com

前端 未结 3 404
名媛妹妹
名媛妹妹 2020-12-13 16:01

Imgur is a image uploading website who offers an API to upload

My code looks exactly like the PHP code they provide as an example. however, in their php code they ar

相关标签:
3条回答
  • 2020-12-13 16:33

    Here's an updated version of dtb's answer for the v3 API using anonymous uploading (you need to register your app at http://api.imgur.com/ to get your client ID):

    using (var w = new WebClient())
    {
        string clientID = "<<INSERT YOUR ID HERE>>";
        w.Headers.Add("Authorization", "Client-ID " + clientID);
        var values = new NameValueCollection
        {
            { "image", Convert.ToBase64String(File.ReadAllBytes(@"hello.png")) }
        };
    
        byte[] response = w.UploadValues("https://api.imgur.com/3/upload.xml", values);
    
        Console.WriteLine(XDocument.Load(new MemoryStream(response)));
    }
    

    And the response is now like this (see http://api.imgur.com/models/image):

    <data success="1" status="200">
        <id>SbBGk</id>
        <title/>
        <description/>
        <datetime>1341533193</datetime>
        <type>image/jpeg</type>
        <animated>false</animated>
        <width>2559</width>
        <height>1439</height>
        <size>521916</size>
        <views>1</views>
        <bandwidth>521916</bandwidth>
        <deletehash>eYZd3NNJHsbreD1</deletehash>
        <section/>
        <link>http://i.imgur.com/SbBGk.jpg</link>
    </data>
    
    0 讨论(0)
  • 2020-12-13 16:41

    I Guess that the dtb solution is deprecated

        using (var w = new WebClient())
        {
            var values = new NameValueCollection
        {
            {"image", Convert.ToBase64String(imageData)},
            {"type", "base64"}
        };
    
            w.Headers.Add("Authorization", "Client-ID xxxxxxxxx");
           var response = w.UploadValues("https://api.imgur.com/3/image", values);
        }
    

    another way to do:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
            request.Headers.Add("Authorization", "Client-ID xxxxxxx");
            request.Method = "POST";
    
            ASCIIEncoding enc = new ASCIIEncoding();
            string postData = Convert.ToBase64String(imageData);
            byte[] bytes = enc.GetBytes(postData);
    
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
    
            Stream writer = request.GetRequestStream();
            writer.Write(bytes, 0, bytes.Length);
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
    0 讨论(0)
  • 2020-12-13 16:52

    I've just uploaded this image

    Hello World

    using this code:

    using (var w = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "key", "433a1bf4743dd8d7845629b95b5ca1b4" },
            { "image", Convert.ToBase64String(File.ReadAllBytes(@"hello.png")) }
        };
    
        byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);
    
        Console.WriteLine(XDocument.Load(new MemoryStream(response)));
    }
    

    You might want to change your API key now :-)

    The output was:

    <rsp stat="ok">
      <image_hash>IWg2O</image_hash>
      <delete_hash>fQAXiR2Fdq</delete_hash>
      <original_image>http://i.imgur.com/IWg2O.png</original_image>
      <large_thumbnail>http://i.imgur.com/IWg2Ol.jpg</large_thumbnail>
      <small_thumbnail>http://i.imgur.com/IWg2Os.jpg</small_thumbnail>
      <imgur_page>http://imgur.com/IWg2O</imgur_page>
      <delete_page>http://imgur.com/delete/fQAXiR2Fdq</delete_page>
    </rsp>
    
    0 讨论(0)
提交回复
热议问题