batch request fail with 302.
foreach (var item in batchItems) { var contentStream = await graphClient.Drives[_driveId].Items[item.Id].Content.Request().GetAsync();
if (contentStream != null)
{
using (var memoryStream = new MemoryStream())
{
await contentStream.CopyToAsync(memoryStream);
var imageBytes = memoryStream.ToArray();
var resizedImageBytes = ResizeImage(imageBytes, 100, 100); // Resize to 100x100 pixels
if (resizedImageBytes != null)
{
var base64String = Convert.ToBase64String(resizedImageBytes);
stockDropDriveItems.Add(new StockdropDriveItem
{
Name = item.Name,
ImageBase64 = base64String
});
}
}
}
var contentRequest = graphClient.Drives[_driveId].Items[item.Id].Content.Request();
var requestId = contentBatchRequestContent.AddBatchRequestStep(contentRequest);
requestIdToItemIdMap.Add(item.Id, requestId);
}
// Send the batch request to get item content
var contentResponse = await graphClient.Batch.Request().PostAsync(contentBatchRequestContent);
//// Process the responses to get the content
foreach (var item in batchItems)
{
if (requestIdToItemIdMap.TryGetValue(item.Id, out var requestId))
{
var content = await contentResponse.GetResponseByIdAsync(requestId);
using (var reader = new StreamReader(await content.Content.ReadAsStreamAsync()))
{
var contentString = await reader.ReadToEndAsync();
stockDropDriveItems.Add(new StockdropDriveItem
{
Name = item.Name,
DownloadUrl = contentString
});
}
}
}
for this line: var content = await contentResponse.GetResponseByIdAsync(requestId); although the contentReponse is succeeded with 200 code, but GetResponseByIdAsync always is 302
You can use batch operations: https://learn.microsoft.com/en-us/graph/json-batching
You can do multiple queries in parallel (now you seem to be doing them sequentially, just using "await" does not make your requests run in parallel)
You can also use both techniques at the same time.
BTW, there is microsoft graph client SDK with a built-in support for retry strategy, batching, etc. You don't need to do it yourself, you seem to be doing it wrong anyway (you should use graph API response headers to get appropriate delay before the next request can be made)