Imgur上传API [英] Imgur API uploading
问题描述
所以有这条线code的
字符串数据= URLEn coder.en code(图像,UTF-8)+=+ URLEn coder.en code(Base64.en codeBase64String(baos.toByteArray())的toString(),UTF-8);数据+ =&放大器; + URLEn coder.en code(钥匙,UTF-8)+=+ URLEn coder.en code(API键放在这里,UTF-8 );
当我注册了Imgur API我被赋予了CLIENT_ID和client_secret,并想知道我使用的一个地方说,它说:您的API密钥GOES HERE也在第一部分在第二行关键的我该怎么进入呢?也就是上传 http://imgur.com/api/upload 的网站,因为我看到了一些不同的。
尝试了这一点:
公共静态字符串getImgurContent(字符串clientID的)抛出异常{
URL网址;
URL =新的URL(https://api.imgur.com/3/image);
HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection(); 字符串数据= URLEn coder.en code(图像,UTF-8)+=
+ URLEn coder.en code(IMAGE_URL,UTF-8); conn.setDoOutput(真);
conn.setDoInput(真);
conn.setRequestMethod(POST);
conn.setRequestProperty(授权,客户ID+ clientID的);
conn.setRequestMethod(POST);
conn.setRequestProperty(内容类型,
应用程序/ x-WWW的形式urlen codeD); conn.connect();
StringBuilder的STB =新的StringBuilder();
OutputStreamWriter WR =新OutputStreamWriter(conn.getOutputStream());
wr.write(数据);
wr.flush(); //获取响应
RD的BufferedReader =新的BufferedReader(
新的InputStreamReader(conn.getInputStream()));
串线;
而((行= rd.readLine())!= NULL){
stb.append(线).append(\\ n);
}
wr.close();
rd.close(); 返回stb.toString();
}
几乎像矮胖,让每一块重新走到一起,codeS来自世界各地的,至少它的工作正如所料,它的一个耻辱,他们没有例子...
享受。
PS:OU还可以对文件做(还没有尝试过),但你需要将图像转换为base64,然后为utf8(更换网址)
编辑,用这个来代替URL,这样你就可以上传文件:
//创建的base64形象
BufferedImage的图像= NULL;
档案文件=新的文件(IMAGEDIR);
//读取图像
图像= ImageIO.read(文件);
ByteArrayOutputStream的字节数组=新ByteArrayOutputStream();
ImageIO.write(形象,PNG,字节);
字节[] = byteImage byteArray.toByteArray();
字符串dataImage = Base64.en code(byteImage);
字符串数据= URLEn coder.en code(图像,UTF-8)+=
+ URLEn coder.en code(dataImage,UTF-8);
块引用>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.
解决方案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");
这篇关于Imgur上传API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!