如何使用带有UWP的Web客户经理获取用户个人资料照片

Han*_*ang 2 c# microsoft-account uwp

我创建了一个UWP应用程序.我想获得Microsoft帐户的用户个人资料.我使用网络帐户管理器实现登录功能.

我试图实现这一点.:https://msdn.microsoft.com/en-us/windows/uwp/security/web-account-manager

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

Elv*_*SFT 6

我可以获取用户名,ID,帐户ID等(Microsoft帐户).但我无法获得用户个人资料图片.

获取访问令牌后,您可以通过另一个实时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;
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和XAML:

 <Image Name="myProfile"/>
Run Code Online (Sandbox Code Playgroud)

注意:Live API已被OneDrive API取代.因此,强烈建议您在开发过程中使用OneDrive API而不是Live API.