c# - 通过Graph SDK获取用户联系人

标签 c# azure exchange-server microsoft-graph-sdks

我尝试使用 graph sdk 和 c# 读取所有用户联系人,但在响应的用户中,即使用户有联系人,联系人数组始终为空

我使用 graph sdk 和 c# 请求所有用户的联系人在线交换,但是

var graphResult = graphClient.Users.Request().GetAsync().Result;
            Console.WriteLine(graphResult[0].Contacts[0]); 

返回NullReferenceException

我授予了以下权限:

privilidges

以下标记是在azure中设置的 token

在这里你可以看到我的租户id等 apid an so on tenent id

主类

using Microsoft.Graph;
using Azure.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.Data.SqlClient;

namespace ExchangeTestAppKonsole
{
    internal class Program
    {
        static void Main(string[] args)
        {

            getContacts();

            Console.ReadLine();
        }

        public static void getContacts()
        {
            var clientId = "de196208-b4d7-468f-8fa4-7328551566b9";
            var clientSecret = "~uG8Q~~vrTGuaIPfzeIR9GUUpSK5aaG.KZTYGcnD";
            var redirectUri = "https://global.consent.azure-apim.net/redirect";
            var authority = "https://login.microsoftonline.com/0be300e6-91fd-4638-bcd1-40d742ef6ece/v2.0";
            var cca = ConfidentialClientApplicationBuilder.Create(clientId)
                                                          .WithAuthority(authority)
                                                          .WithRedirectUri(redirectUri)
                                                          .WithClientSecret(clientSecret)
                                                          .Build();


            // use the default permissions assigned from within the Azure AD app registration portal
            List<string> scopes = new List<string>();
            scopes.Add("https://graph.microsoft.com/.default");

            var authenticationProvider = new MsalAuthenticationProvider(cca, scopes.ToArray());
            GraphServiceClient graphClient = new GraphServiceClient(authenticationProvider);

            var graphResult = graphClient.Users.Request().GetAsync().Result;
            Console.WriteLine(graphResult[0].Contacts[0]);
        }
    }
}

身份验证提供者

using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ExchangeTestAppKonsole
{
    internal class MsalAuthenticationProvider : IAuthenticationProvider
    {
        private IConfidentialClientApplication _clientApplication;
        private string[] _scopes;

        public MsalAuthenticationProvider(IConfidentialClientApplication clientApplication, string[] scopes)
        {
            _clientApplication = clientApplication;
            _scopes = scopes;
        }

        public async Task AuthenticateRequestAsync(HttpRequestMessage request)
        {
            var token = await GetTokenAsync();
            request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
        }

        public async Task<string> GetTokenAsync()
        {
            AuthenticationResult authResult = null;
            authResult = await _clientApplication.AcquireTokenForClient(_scopes).ExecuteAsync();
            return authResult.AccessToken;
        }
    }
}

我还通过使用第一个用户登录 graphExplorer 来请求该用户的联系人 并请求/me/contacts 端点,它显示 3 个联系人

这似乎是一个前提问题,但我不知道问题到底是什么。

最佳答案

ContactsUser 资源类型上的关系。

如果您想在响应中包含某些关系,则需要在Expand中指定关系。

并非所有关系和资源都支持扩展。根据documentation没有提到 contacts 支持扩展,因此此代码可能无法工作。

var graphResult = graphClient.Users
                   .Request()
                   .Expand("contacts")
                   .GetAsync().Result();
        Console.WriteLine(graphResult[0].Contacts[0]);

在这种情况下,您需要为每个用户单独调用以获取联系人。

var contacts = await graphClient.Users["{user_id}"].Contacts
    .Request()
    .GetAsync();

Users relationships

关于c# - 通过Graph SDK获取用户联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75112614/

相关文章:

Azure 突触 : Merge command with the identity column in target table is not working

powershell - Test-ServiceHealth输出显示

powershell - 表达式或语句 powershell 中的意外标记 'if'

sql - Azure 中具有聚集索引的表出现聚集索引错误

c# - 在 C# 中交换 Powershell

C# 泛型方法返回值

c# - 为什么异步方法调用者的行为很奇怪?

c# - 有关 IEnumerable.GetEnumerator() 的错误消息

c# - 组合框任务数据绑定(bind)方式 : using previous combo box to set the where condition

azure - 枚举 Azure 中云服务的所有实例