简体   繁体   English

通过 Graph SDK 获取用户联系人

[英]Get users contacts by Graph SDK

I try to read all users contacts by using graph sdk and c# but in the user at the response always the array of contacts is null even though the user has contacts我尝试使用图形 sdk 和 c# 来读取所有用户联系人,但在响应中的用户中,即使用户有联系人,联系人数组始终为 null

I was requesting all user's contacts from exchange online with graph sdk and c#, but我正在使用图表 sdk 和 c# 从在线交换中请求所有用户的联系人,但是

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

returns NullReferenceException .返回NullReferenceException

I granted following privileges:我授予以下特权:

特权

the following token is set in azure以下令牌设置在 azure令牌

here you can see my tenant id and so on在这里你可以看到我的租户 ID 等等快速等等租户编号

Main Class主 Class

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]);
        }
    }
}

Authentication Provider身份验证提供者

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;
        }
    }
}

I also requested the contacts of the first user by logging in with this user into graphExplorer and requested the /me/contacts endpoint it shows 3 contacts我还通过与该用户一起登录到 graphExplorer 请求了第一个用户的联系人,并请求了它显示 3 个联系人的 /me/contacts 端点

it seems to be a premissions thing but i've no idea what exactly the problem is.这似乎是前提条件的事情,但我不知道到底是什么问题。

Contacts is a relationship on User resource type. ContactsUser资源类型上的关系。

If you want to include some relationship in the response, you need to specify the relationship in Expand .如果你想在响应中包含一些关系,你需要在Expand中指定关系。

Not all relationships and resources support the expand.并非所有关系和资源都支持扩展。 According to the documentation there is no mention that contacts supports expand, so probably this code won't work.根据文档,没有提及contacts支持扩展,因此这段代码可能无法正常工作。

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

In that case you need to make a separate call for each user to get contacts.在这种情况下,您需要为每个用户单独拨打电话以获取联系人。

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

Users relationships 用户关系

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

相关问题 仅获取具有 TransitiveMembers Microsoft Graph C# SDK 的用户 - Get only users with TransitiveMembers Microsoft Graph C# SDK 从活动目录组获取联系人和用户 - get contacts and users from active directory group 如何获取联系人或向用户添加Exchange邮箱? - How to get or add contacts to users Exchange mailbox? 使用 Microsoft Graph SDK 从 Azure 应用程序获取所有用户及其角色(包括名称和值) - Get all users and their roles(including name and value) from Azure application using Microsoft Graph SDK 通过Lync2013 SDK中的1个搜索获取多个联系人 - Get Multiple Contacts from 1 search in Lync2013 SDK 从Active Directory中的组获取​​所有用户和联系人 - Get all users and contacts from a group in Active Directory 如何从 Azure Ad graph api 获取联系人 - How to get contacts from Azure Ad graph api 从所有Outlook联系人文件夹Microsoft Graph获取联系人 - Get Contacts from All Outlook Contact folders Microsoft Graph 使用 Graph SDK 获取 Outlook 电子邮件文件夹和电子邮件 - Get Outlook EmailFolders and Emails with Graph SDK Microsoft Graph SDK .NET获取范围内的事件 - Microsoft Graph SDK .NET get events in range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM