【问题标题】:Imgur API uploadingImgur API 上传
【发布时间】:2013-06-25 12:40:24
【问题描述】:

于是就有了这行代码

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 KEY GOES HERE”。键”我在那里输入什么?也是上传它的网站http://imgur.com/api/upload,因为我见过一些不同的。

【问题讨论】:

    标签: java api imgur


    【解决方案1】:

    试试这个:

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

    【讨论】:

    【解决方案2】:

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

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

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

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

    编辑:您可能会发现以下有用 - Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401

    【讨论】:

    • 我必须让上传工作(所以我想是这样),我使用的上传链接(没有错误)是api.imgur.com/2/upload.xmlimgur.com/api/upload.xml,但那是用一个键我发现,问题是我也无法解析响应以获取上传的链接。
    • 啊,是的,我已经用我在其他地方找到的钥匙尝试过它并且它有效。
    • 抱歉没有意识到按回车会添加评论。您说您获得了 client_id 和 client_secret,这不适用于您的代码,因为我认为您使用的是 API 的第 2 版是正确的。你可以告诉这一点,因为你的上传 URL 包含 /2/,而我展示给你的那个是 /3/。正如您所说,API 的第 2 版使用了“密钥”,但在第 3 版中,他们将其交换为使用客户端 ID 和客户端密码。它们不再提供“密钥”,并且不适用于 API 版本 3。
    • 你知道如何使用java版本3上传图片吗?
    • 我不懂 Java,抱歉。在过去的几天里,我只是对 API 有点熟悉。可惜 Imgur 自己没有代码示例了
    猜你喜欢
    • 2019-12-05
    • 2013-04-17
    • 1970-01-01
    • 2013-08-07
    • 2010-12-21
    • 2014-04-28
    • 1970-01-01
    • 2013-12-29
    • 2016-07-17
    相关资源
    最近更新 更多