简体   繁体   中英

How do I upload to an imgur user's account

I am able to upload anonymous images to imgur using the api like this:

public static void UploadImage()
{
    Image image = GetImage();
    string base64 = Util.ImageToBase64(image);

    using (WebClient client = new WebClient())
    {
        client.Headers.Add("Authorization", "Client-ID " + clientID);

        NameValueCollection data = new NameValueCollection();
        data["image"] = base64;
        data["type"] = "base64";

        byte[] responsePayload = client.UploadValues("https://api.imgur.com/3/image/", "POST", data);
        string response = Encoding.ASCII.GetString(responsePayload);
    }
}

It's uploading to a completely random url. I couldn't find any documentation which showed how to do this, but is it possible to upload an image to a user's account so that it shows in their uploaded images?

I figured it out. Instead of sending a "CLIENT-ID" header, you need to send a "BEARER" header, with the token that you recieve by using imgur's oauth 2 api . This allows you to post images to a user's account, whereas before I only providewd the client id, which allowed me to post anonymous images. The code looks like this:

client.Headers.Add("Authorization", "BEARER " + accessToken);

要添加到 Ivan Chub 的答案......我只是在 python 中上传到我的 imgur 帐户一分钟,直到我尝试将 BEARER 更改为 Bearer。

req.add_header("Authorization", "Bearer " + imgur_access_token)

If using curl from command line

curl -X POST -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -F "image=@PATH_TO_YOUR_IMAGE_FILE" https://api.imgur.com/3/upload

More info on https://planspace.org/2013/01/13/upload-images-to-your-imgur-account/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM