2

So there is this line of code

String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");

data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(YOUR API KEY GOES HERE, "UTF-8");

and when I registered for the Imgur API I was given a client_id and a client_secret and was wondering which one I use for where it says "YOUR API KEY GOES HERE" also in the first part in the second line where it says "key" what do I enter there? Also is the site to upload it http://imgur.com/api/upload because I have seen a few different ones.

user2526311
  • 1,004
  • 2
  • 10
  • 19

2 Answers2

8

try this out:

    public static String getImgurContent(String clientID) throws Exception {
    URL url;
    url = new URL("https://api.imgur.com/3/image");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String data = URLEncoder.encode("image", "UTF-8") + "="
            + URLEncoder.encode(IMAGE_URL, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    return stb.toString();
}

was almost like humpty dumpty, getting every piece back together, codes from everywhere, at least it worked as expected, its a shame they don't have examples...
enjoy.

ps: ou can also make with FILES (haven't tried yet) but you need to convert an image to base64 and then to utf8 (to replace the url)

edit, use this instead of the URL, so you can upload files:

  //create base64 image
    BufferedImage image = null;
    File file = new File(imageDir);
    //read image
    image = ImageIO.read(file);
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    ImageIO.write(image, "png", byteArray);
    byte[] byteImage = byteArray.toByteArray();
    String dataImage = Base64.encode(byteImage);
    String data = URLEncoder.encode("image", "UTF-8") + "="
    + URLEncoder.encode(dataImage, "UTF-8");
Simego
  • 431
  • 5
  • 16
1

The site to upload to is - https://api.imgur.com/3/image or you can alternatively use the same link with "upload" instead of image.

I am currently trying to use the Imgur API myself and although I have not got it completely right yet (I can't seem to parse the URL response) I have looked at quite a few code examples for it. Are you definitely using version 3 of the API?

Because the homepage of the API says that you should give your client ID in this format "Authorization Client-ID YOUR_CLIENT_ID", not using "key" like you are.

Have a look at http://api.imgur.com/

Edit: you might find the following useful - Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401

Community
  • 1
  • 1
Karl Green
  • 260
  • 1
  • 4
  • 11
  • I got to get the uploading working (so I think so) and the link to upload I used (which provided me with no error) was http://api.imgur.com/2/upload.xml and http://imgur.com/api/upload.xml but that was with a key that I found, the problem was I also am having trouble parsing the response to get the uploaded link. – user2526311 Jun 28 '13 at 00:51
  • Ah yeah I've tried it with a key that I found somewhere else and it worked. – Karl Green Jun 28 '13 at 12:42
  • Sorry didn't realise pressing enter would add the comment. You say you were given a client_id and a client_secret, that won't work with your code because I was right in thinking that you're using version 2 of the API. You can tell this because your upload URL contains /2/ whereas the one I showed you has /3/. Version 2 of the API used a "key" as you're talking about but in version 3 they swapped that to use a client ID and client secret instead. They no longer give out "keys" and they do not work with version 3 of the API. – Karl Green Jun 28 '13 at 13:00
  • Do you know how to upload images using version 3 with java? – user2526311 Jun 28 '13 at 16:51
  • I don't know Java, sorry. I am just vaguely familiar with the API after looking at it for the past few days. It's a shame that Imgur themselves don't have code examples anymore – Karl Green Jun 28 '13 at 22:09