4

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

Andy Evans
  • 6,997
  • 18
  • 72
  • 118
  • 1
    first I would use Fiddler or similar to check that the request is being formed correctly as you expect, in case the authorisation data (i.e. token and secret) is not being sent correctly or something – ADyson May 26 '17 at 13:51
  • @Andy Did you figure out the issue? I see that you are not using `refresh_token` or `access_token` to authorize the user. – Govind Kailas May 17 '18 at 14:19
  • @GovindKailas, I found out that you also have similar kind of issue. Did you get any solution. I'm having same error, my code is in php. – Parth Panchal Nov 22 '18 at 12:39
  • No @ParthPanchal I couldn't find any solution to upload an image to own album. Left it – Govind Kailas Nov 23 '18 at 03:52
  • I have the same issue using just `curl` in command line... – Michal Przybylowicz Sep 12 '19 at 13:18

1 Answers1

1

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.

shiraz27
  • 1,898
  • 18
  • 14