如何使用UWP的Web帐户管理器获取用户个人资料图片

问题描述:

我创建了一个UWP应用程序。我想用Microsoft帐户获取用户配置文件。我使用网络帐户管理器实现登录功能。如何使用UWP的Web帐户管理器获取用户个人资料图片

我试过去实现这个目的。 :https://msdn.microsoft.com/en-us/windows/uwp/security/web-account-manager

我可以获取用户名,ID,帐户ID等(Microsoft帐户)。 但我无法获取用户个人资料图片。我试图使用USER类(使用Windows.System.User),但我不知道如何连接这两个项目。 用户等级:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/UserInfo 如何在UWP应用程序中获取用户个人资料图片?

我可以获取用户名,ID,帐户ID等(微软帐户)。但我无法获取用户个人资料图片。

获得访问令牌后,您可以通过另一个现场的API获取用户的个人资料图片:

private async void GetUserProfileImageAsync(String token) 
{ 
    var photoApi = new Uri(@"https://apis.live.net/v5.0/me/picture?access_token=" + token); 
    using (var client = new HttpClient()) 
    { 
     var photoResult = await client.GetAsync(photoApi); 
     using (var imageStream = await photoResult.Content.ReadAsStreamAsync()) 
     { 
      BitmapImage image = new BitmapImage(); 
      using (var randomStream = imageStream.AsRandomAccessStream()) 
      { 
       await image.SetSourceAsync(randomStream); 
       myProfile.Source = image; 
       myProfile.Width = image.PixelWidth; 
       myProfile.Height = image.PixelHeight; 
      } 

     } 
    } 
} 

和XAML:

<Image Name="myProfile"/> 

注:实时API已取代OneDrive API 。因此强烈建议您在开发过程中使用OneDrive API而不是Live API。