首先,此示例代码是在 .net core 中创建的,您应该通过以下代码在配置中设置 GraphScopes:
"GraphScopes": "User.Read User.ReadBasic.All Mail.Send MailBoxSettings.ReadWrite Contacts.ReadWrite"
另请注意,如果有多个文件夹,ContactFolders 只会返回结果。永远不会返回默认的联系人文件夹。如果用户没有其他文件夹,这将返回空结果。如果要获取主文件夹和需要分别获取的附加文件夹,则合并结果。
// Get the defaultContacts
var defaultContacts = await graphClient
.Me
.Contacts
.Request()
.GetAsync();
// Get the contactFolders
var contactFolders = await graphClient
.Me
.ContactFolders
.Request()
.GetAsync();
// Use this to store the contact from all contact folder.
List<Contact> contactFolderContacts = new List<Contact>();
if (contactFolders.Count > 0) {
for (int i = 0; i < contactFolders.Count; i++) {
var folderContacts = await graphClient
.Me
.ContactFolders[contactFolders[i].Id]
.Contacts
.Request()
.GetAsync();
contactFolderContacts.AddRange(folderContacts.AsEnumerable());
}
// This will combine the contact from main folder and the additional folders.
contactFolderContacts.AddRange(defaultContacts.AsEnumerable());
} else {
// This user only has the default contacts folder
contactFolderContacts.AddRange(defaultContacts.AsEnumerable());
}
// Use this to test the result.
foreach (var item in contactFolderContacts) {
Debug.WriteLine("first:" + item.EmailAddresses);
}
慕尼黑8549860
神不在的星期二
相关分类