Imgur API上传

所以有这行代码

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"); 

当我注册Imgur API时,我得到了一个client_id和一个client_secret,并想知道我在哪里使用哪个“你的API密钥就在这里”也在第二行的第一部分,它说“关键”是什么我进入那里? 也是上传它的网站http://imgur.com/api/upload因为我看过几个不同的网站。

试试这个:

  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(); } 

几乎像是矮胖子,让每一件都重新组合起来,来自世界各地的代码,至少它按预期工作,很遗憾他们没有例子……
请享用。

ps:你也可以使用FILES(尚未尝试),但你需要将图像转换为base64然后转换为utf8(以替换url)

编辑,使用此而不是URL,以便您可以上传文件:

  //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"); 

要上传的网站是 – https://api.imgur.com/3/image ,您也可以使用“上传”而不是图片的相同链接。

我目前正在尝试自己使用Imgur API,虽然我还没有完全正确(我似乎无法解析URL响应)我已经查看了相当多的代码示例。 你肯定使用的是API的第3版吗?

因为API的主页说您应该以“Authorization Client-ID YOUR_CLIENT_ID”格式提供您的客户端ID,而不是像您一样使用“密钥”。

看看http://api.imgur.com/

编辑:您可能会发现以下有用 – 匿名上载文件对象到Imgur API(JSON)给出了身份validation错误401