简体   繁体   English

使用REST请求从Office365的Delve个人资料中获取照片

[英]Get photo from Delve profile of Office365 using REST request

I have a problem with getting profile image from Delve account. 我从Delve帐户获取个人资料图片时遇到问题。 Here is the example of link which returns me photo when i put it into browser: https://orgname.sharepoint.com/_vti_bin/DelveApi.ashx/people/profileimage?userId=alias@org.com&size=L 这是链接的示例,当我将其放入浏览器时,该链接会返回我的照片: https : //orgname.sharepoint.com/_vti_bin/DelveApi.ashx/people/profileimage?userId=alias@org.com&size=L

Now I need to get this photo from code. 现在,我需要从代码中获取这张照片。 I tried such way, that works perfect for external images not in Office365: 我尝试过这种方式,非常适合不在Office365中使用的外部图像:

 var credentials = new NetworkCredential("myemail", "password");
 using (var handler = new HttpClientHandler { Credentials = credentials })
 using (var client = new HttpClient(handler))
 {
     var bytes = await client.GetByteArrayAsync(url);
     return Convert.ToBase64String(bytes);
 }

But as responce I get html page with text like: 但是作为响应,我得到了带有以下内容的html页面:

<H1>We can't sign you in</H1><p>Your browser is currently set to block cookies. You need to allow cookies to use this service.</p><p>Cookies are small text files stored on your computer that tell us when you're signed in. To learn how to allow cookies, check the online help in your web browser.</p>

I think it is related with Office365 Authorization, but I don`t know how to perform REST request to this url with my credentials... 我认为这与Office365授权有关,但我不知道如何使用我的凭据对此URL执行REST请求...

Problem Solved, first we need to initialize SharePoint Context: 解决问题后,首先我们需要初始化SharePoint上下文:

public ClientContext SetupSpContext()
    {
        // This builds the connection to the SP Online Server
        var clientContext = new ClientContext(_sharePointCrmDocumentsSiteName);
        var secureString = new SecureString();
        foreach (var c in _sharePointCrmDocumentsPwd.ToCharArray()) secureString.AppendChar(c);
        {
            clientContext.Credentials = new SharePointOnlineCredentials(_sharePointCrmDocumentsLoginName, secureString);
        }
        var web = clientContext.Web;
        clientContext.Load(web);
        clientContext.ExecuteQuery();
        return clientContext;
    }

Then we can Get profile picture from Shrepoint using User email: 然后,我们可以使用用户电子邮件从Shrepoint获取个人资料照片:

public string DownloadProfilePictureAsBase64(string email)
    {
        try
        {
            var pictureUrl = GetPictureUrl(email);

            var fileInfo = File.OpenBinaryDirect(_sharePointContext, pictureUrl);
            using (var memory = new MemoryStream())
            {
                var buffer = new byte[1000000];

                int nread;
                while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    memory.Write(buffer, 0, nread);
                }
                memory.Seek(0, SeekOrigin.Begin);

                var buffer2 = new byte[memory.Length];

                memory.Read(buffer2, 0, buffer2.Length);
                return Convert.ToBase64String(buffer2);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Picture for user {email} can not be downloaded");
            Console.WriteLine(ex.Message);
        }
        return null;
    }

    private string GetPictureUrl(string email)
    {
        string targetUser = $"i:0#.f|membership|{email}";

        var peopleManager = new PeopleManager(_sharePointContext);
        var personProperties = peopleManager.GetPropertiesFor(targetUser);
        _sharePointContext.Load(personProperties, p => p.PictureUrl);
        _sharePointContext.ExecuteQuery();
        var pictureUri = new Uri(personProperties.PictureUrl);
        var localPath = pictureUri.LocalPath.Replace("MThumb", "LThumb"); //Change size of the picture

        return localPath;
    }

You should Microsoft Graph to get a user's picture. 您应该使用Microsoft Graph来获取用户的图片。 https://graph.microsoft.com/v1.0/me/photo gets your metadata about the profile picture, and https://graph.microsoft.com/v1.0/me/photo/ $value gets you the image. https://graph.microsoft.com/v1.0/me/photo获取有关个人资料图片的元数据,而https://graph.microsoft.com/v1.0/me/photo/ $ value获取您的图像。 You need to use OAuth. 您需要使用OAuth。 See https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get for more details. 有关更多详细信息,请参见https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM