[英]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. Contacts
是User
资源类型上的关系。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.