There is a post on imgur that is sort of like my question, but it’s using jquery and I don’t want to just copy and paste it. I’ve tried implementing what I saw into my code though.
I can upload an image to my imgur account, But I can’t upload it to an album I have. I’ve tried implementing some code I’ve seen like:
var fd = new FormData();
fd.append(image, img);
fd.append(album, albumId);
...
xhr.send(fd);
and something like:
var payload = {image: img, album: albumId};
...
xhr.send(payload);
and using every combination of url I could, but nothing uploads the image to the album.
The only thing that works is: xhr.send(img);, and no matter what url I use, it only uploads it to my account, not my album.
The docs say to use this url: https://api.imgur.com/3/album/{{albumHash}}/add
so I changed albumhash to my albums id and tried sending the image, but it only uploads it to my account not my album.
My code:
function upload(img)
{
imgur_access_token = 'access token';
albumId = 'album id';
url = 'https://api.imgur.com/3/album/' + albumId + '/add';
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('POST', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + imgur_access_token);
xhr.setRequestHeader('Content-Type', 'multipart/form-data;');
xhr.onload = function()
{
var response = xhr.response;
if (response.success)
{
var link = response.data.link;
console.log('link: ' + link);
}
else
{
console.log('not successful');
}
}
xhr.send(img);
}
So, my question is - How come the code in question that I linked works, but I haven’t had any success with mine? What should I change in my code to get it working?
Based on my read of the documentation, you don’t actually upload an image at this endpoint. Instead, you upload at the image upload endpoint before then adding that image to an album at the album add endpoint you’re trying to use here.
When you post the image, the response will include the ID. Pull that out of the response and use it in the body of your request to the album add endpoint to include that image in your album.
I’m a little confused. I got the image id, but where do I upload the image to the album? So I’ll have 2 urls - image and album id/add. open with the image url, then use the link and open with the album url and send the image?
I meant like how would I go about this. Like Post again using the album url and sending the link? Because When I tried that it didn’t work, so I’m kinda just more confused about this.
… I’m gonna be honest. You’re not helping much at all.
I see in the documentation (add images to an album - authed) that I create and array of ids and add the link to it.
Again, do I xhr.open with the album url and then do I send the image or the ids after? I tried this and it didn’t work, so obviously that isn’t right and I still don’t know what to do.
I also tried creating a formdata and sending the formdata with the id. That didn’t work either…so I’m no closer to solving this then I was when I asked this question.
Sorry if I’m sounding like a broken record, it’s just you keep sending me the same links to the docs that I’ve already seen and it’s obviously not helping. I can upload the image to imgur, but I don’t see how to add that image to an album.
The most recent code you posted should work if you add your auth header on the add to album request (just like you do on the first request, but you’ll have to do it again on the second). The 401 you’re getting back is the error code for Unauthorized. You might need to set the content type header on the second request too.
Just noticed you can do this with a single request by adding the album ID when you upload the image. Check the album parameter on the image upload endpoint documentation.
But I can’t upload it to the album with the xhr.setRequestHeader('Content-Type', 'multipart/form-data;'); and using form data or just sending the image…
It works, but I’m not sure if it’s right…The docs say to use the multipart/form-data request header and the above code seems a little slow posting.