Reputation: 35
I'm trying to use the Imgur API to create an album and have images attached in one request using my authenticated account. I first upload several images (which returns their image IDs), then I call my album-creation endpoint with those image IDs in the "ids[]" field (along with title, description, cover, and privacy parameters). However, while the API call returns success and an album URL is generated, the album is empty (i.e. no images are inside).
// Upload images to Imgur and return both id and deletehash.
public async Task<(string id, string deletehash)> UploadImageAsync(string filePath, string accessToken)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var content = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(fileContent, "image", Path.GetFileName(filePath));
var response = await client.PostAsync("https://api.imgur.com/3/upload", content);
var jsonResponse = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<dynamic>(jsonResponse);
if ((bool)result.success)
{
string id = result.data.id.ToString();
string delHash = result.data.deletehash?.ToString();
return (id, delHash);
}
else
{
string error = result.data?.error?.ToString() ?? "Unknown error";
throw new Exception($"Failed to upload image: {error}");
}
}
}
}
// Create an album with images (using the authenticated endpoint).
public async Task<(string AlbumId, string AlbumDeleteHash)> CreateAlbumAsync(string accessToken, List<string> imageIds)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var content = new MultipartFormDataContent())
{
// Add each image id using key "ids[]"
foreach (var id in imageIds)
{
content.Add(new StringContent(id), "ids[]");
}
// Add a title, description, cover, and privacy settings.
content.Add(new StringContent("My dank meme album " + Guid.NewGuid().ToString()), "title");
content.Add(new StringContent("This album contains a lot of dank memes. Be prepared."), "description");
content.Add(new StringContent(imageIds[0]), "cover");
content.Add(new StringContent("hidden"), "privacy");
var response = await client.PostAsync("https://api.imgur.com/3/album", content);
var jsonResponse = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Failed to create album: {jsonResponse}");
}
dynamic result = JsonConvert.DeserializeObject(jsonResponse);
if ((bool)result.success)
{
string albumId = result.data.id.ToString();
string albumDeleteHash = result.data.deletehash.ToString();
return (albumId, albumDeleteHash);
}
else
{
string error = result.data.error?.ToString() ?? "Unknown error";
throw new Exception($"Failed to create album: {error}");
}
}
}
}
private async Task<string> ProcessLineAsync(string email, string accessToken)
{
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "photos", email);
if (!Directory.Exists(folderPath))
throw new Exception($"Folder not found: {folderPath}");
var desiredOrder = new List<string>
{
"image1.jpg",
"image2.jpg",
"image3.jpg",
"image4.jpg",
"image5.jpg"
};
var imageFiles = desiredOrder
.Select(name => Path.Combine(folderPath, name))
.Where(File.Exists)
.ToList();
if (imageFiles.Count == 0)
throw new Exception("No images found to upload.");
List<string> imageIds = new List<string>();
foreach (var imageFile in imageFiles)
{
var (id, deletehash) = await UploadImageAsync(imageFile, accessToken);
imageIds.Add(id);
}
// Create the album and include the images.
var (albumId, albumDeleteHash) = await CreateAlbumAsync(accessToken, imageIds);
return $"https://imgur.com/a/{albumId}";
}
My questions are:
I have verified that:
Any guidance or code corrections would be greatly appreciated!
I was aiming for the following workflow:
Upload Images: I upload each image using the Imgur API’s /upload endpoint and collect their IDs.
Create Album: I create an album via the /album endpoint, including the image IDs in the ids[] parameter along with a title, description, and cover image.
Update Album: (Alternatively, I also tried calling the /album/{albumId}/add endpoint to update the album with the image IDs.)
I expected that after these steps the album URL would display an album containing all the uploaded images. However, the album is created successfully, yet it appears empty (no images are attached).
Upvotes: 0
Views: 18