Using Imgur API 3 to upload an image reports that I'm not the owner of the album

Viewed 916

I created an Imgur account and acquired my client ID and secret key before creating an album. My goal was to write in a test environment before integrating it into my application.

Below is final version of the code I have used up until now:

string base64String;
string message;
string album = "abcxyz";

using (Image image = Image.FromFile("c:\\path_to\\image.jpg"))
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        byte[] imageBytes = m.ToArray();
        base64String = Convert.ToBase64String(imageBytes);
    }
}

using (var client = new WebClient())
{
    string clientID = "supersecret";
    client.Headers.Add("Authorization", $"Client-ID {clientID}");
    System.Collections.Specialized.NameValueCollection valueCollection = new System.Collections.Specialized.NameValueCollection();
    valueCollection["image"] = base64String;
    valueCollection["type"] = "base64";
    valueCollection["album"] = album;
    try
    {
        byte[] response = client.UploadValues("https://api.imgur.com/3/upload", "POST", valueCollection);
        message = Encoding.ASCII.GetString(response);
    }
    catch (Exception ex)
    {
        message =  ex.Message;
    }
}

When I run the code I get the message back that I don't apparently own the album that I'm trying to. Specifically:

error=You are not the owner of album '<album hash>', which means you can't 
add images to it. For anonymous albums, use the album deletehash.

I had thought that since I created the album while logged on with my account that it should work - and now I'm missing something. Any extra eyes on this would be greatly appreciated thank you.

Andy

1 Answers

It was an easy peasy 2 step process. but imgur api docs made it very hard for me.

this is in JS: so you should

⠀ 1. make sure you have these headers and this body and a correct + supported type (The type of the file that's being sent; file, base64 or URL) for me base64

var myHeaders = new Headers();
var formdata = new FormData();
myHeaders.append(
    "Authorization",
    "Bearer 8efbea9b750ad38aeb2e8384a378456363ddddd" //your access-token
  );
formdata.append("image", res);
    formdata.append("type", "base64");
    formdata.append("name", any_string_you_want);
    formdata.append("album", "2ddl3LUD");
    var requestOptions = {
      method: "POST",
      headers: myHeaders,
      body: formdata,
    };
  1. make sure you don't include any other extra headers or meta-data like, Client-ID : your-client-id.

extra: if you don't have an access token, generate one by going back to imgur docs, if you don't have an album id, google how to get it.

Related