使用Graph API批量检索联系人和个人资料照片

时间:2020-01-03 06:57:30

标签: c# azure-active-directory microsoft-graph-api microsoft-graph-mail

通过以下查询,我们可以使用Microsoft Graph获取联系人的个人资料图片:

https://graph.microsoft.com/v1.0/users/{user-name}/contacts/{id}/photo/$value

使用上面的查询要花费大量时间来检索大量联系人。有没有办法批量检索联系人和个人资料图片?

借助下面的批处理请求,可以成功以20(最大)的批次成功提取联系人,但是对于个人资料照片,它返回body。我无法处理此body内容。是否有任何方法可以将此body处理为image格式,可以处理。

任何C#API(如果有的话)都会有很大帮助。

Batch_Request

下面是响应。 body内容没有太大帮助。如何处理?

Response

1 个答案:

答案 0 :(得分:1)

这里的body是照片的内容,但使用base64编码。您可以直接将其另存为文件。

这是我来自Graph API响应的演示数据:

enter image description here

使用下面的c#代码将其另存为图像:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var imgBody = "<body value here>";
            byte[] bytes = Convert.FromBase64String(imgBody);
            using (Image image = Image.FromStream(new MemoryStream(bytes)))
            {
                image.Save("d:/test.jpg", ImageFormat.Jpeg);
            }
        }
    }
}

结果: enter image description here enter image description here