首页 > 解决方案 > 使用 Microsoft 图形 API 我想要用户个人资料照片

问题描述

嗨,我正在尝试获取用户照片,已使用

var tenantId = configuration.GetSection("AzureAd").GetSection("TenantId").Value;
               var clientId = configuration.GetSection("AzureAd").GetSection("ClientId").Value;
             var clientSecret = configuration.GetSection("AzureAd").GetSection("clientSecret").Value;
               var InviteRedirectUrl = configuration.GetSection("AzureAd").GetSection("InviteRedirectUrl").Value;
               
                
               var Instance = configuration.GetSection("AzureAd").GetSection("Instance").Value;
               var URL = Instance + tenantId + "/v2.0";

               var scopes = new string[] { "https://graph.microsoft.com/.default" };

               var confidentialClient = ConfidentialClientApplicationBuilder
                      .Create(clientId)
                      .WithAuthority(URL)
                      .WithClientSecret(clientSecret)
                      .Build();


               GraphServiceClient graphServiceClient =
                   new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
                   {


                       var authResult = await confidentialClient
                       .AcquireTokenForClient(scopes)
                       .ExecuteAsync();


                       requestMessage.Headers.Authorization =
                       new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                   })
                   );
Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();  

我收到以下错误

代码:BadRequest 消息:当前经过身份验证的上下文对此请求无效。当向需要用户登录的端点发出请求时,就会发生这种情况。例如,/me 需要登录用户。代表用户获取令牌以向这些端点发出请求。对移动和本机应用程序使用 OAuth 2.0 授权代码流,对单页 Web 应用程序使用 OAuth 2.0 隐式流。内部错误:

如何解决?

标签: microsoft-graph-api

解决方案


推荐阅读