将用户从目录添加到联系人
Adding users from directory to contacts
我正在尝试将 Gsuite 目录中的用户添加为目录中所有用户的联系人。
var credential = GetCredentialForUser(User.PrimaryEmail);
// Create Drive API service.
var peopleService = new PeopleServiceService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
PeopleResource.ListDirectoryPeopleRequest peopleRequest =
peopleService.People.ListDirectoryPeople();
peopleRequest.ReadMask = "names,emailAddresses,addresses,phoneNumbers";
peopleRequest.Sources = PeopleResource.ListDirectoryPeopleRequest.SourcesEnum.DIRECTORYSOURCETYPEDOMAINPROFILE;
peopleRequest.PageSize = 1000;
//peopleRequest.SortOrder = (PeopleResource.ConnectionsResource.ListRequest.SortOrderEnum)1;
var res = peopleRequest.Execute();
var list = peopleService.ContactGroups.List().Execute();
//var directoryPeople = peopleService.People.ListDirectoryPeople().Execute();
var person = res.People.FirstOrDefault();
foreach (var label in list.ContactGroups)
{
Console.WriteLine(label.ResourceName);
}
var all = list.ContactGroups.Where(s => s.Name== "myContacts").FirstOrDefault();
var members = peopleService.ContactGroups.Members.Modify(new ModifyContactGroupMembersRequest() { ResourceNamesToAdd = new List<string> { person.ResourceName }, ResourceNamesToRemove = new List<string> { } }, all.ResourceName).Execute();
我收到异常
{"Google.Apis.Requests.RequestError\r\nResource name \"people/XXXYYYY\" is not a valid contact person resource. [400]\r\nErrors [\r\n\tMessage[Resource name \"people/XXXYYYY\" is not a valid contact person resource.] Location[ - ] Reason[badRequest] Domain[global]\r\n]\r\n"}
这是否意味着我们无法使用目录中的人 api 添加用户作为联系人,如果是这样,最好的批量添加方法是什么?清除所有联系人并从目录中添加新用户?
问题是您正在尝试将配置文件添加到联系人组。您只能将联系人添加到联系人组。所以你应该只用个人资料电子邮件创建一个联系人。
这是我设置批量请求以创建联系人的函数。
将所有目录联系人转换为 Person,然后将其与 BatchRequest 一起发送。
最后执行批处理请求以创建联系人。
确保在 Person 中包含一个主要电子邮件地址。请注意,我检查了 ClientData - 这是我存储我的应用程序名称和 AD 的用户唯一 ID 以跟踪我的应用程序添加的内容的地方。
public Person CreateContact(BatchRequests batchRequest, Person contact, List<string> errors)
{
if (contact.EmailAddresses == null ||
contact.EmailAddresses.Count == 0 ||
!contact.EmailAddresses.Any(e => e.Metadata.Primary == true))
throw new Exception("Primary Email Address Not Found.");
try
{
if (!batchRequest.BatchCreateContactsRequest.Contacts.Any(c => c.ContactPerson.ClientData == contact.ClientData))
{
ContactToCreate contactToCreate = new ContactToCreate();
contactToCreate.ContactPerson = contact;
batchRequest.BatchCreateContactsRequest.Contacts.Add(contactToCreate);
}
}
catch (Exception ex)
{
// CurrentPersonEmail is the user's email whose contacts are being modified.
errors.Add($"Error;{CurrentPersonEmail};{ex.Message}. StackTrace:{ex.StackTrace}");
}
return contact;
}
准备好所有联系人后 - 然后执行请求(这不是完整的代码,但我希望你知道如何调用它):
BatchCreateContactsResponse batchCreateContactsResponse = PeopleService.People.BatchCreateContacts(BatchCreateContactsRequest).Execute();
我正在尝试将 Gsuite 目录中的用户添加为目录中所有用户的联系人。
var credential = GetCredentialForUser(User.PrimaryEmail);
// Create Drive API service.
var peopleService = new PeopleServiceService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
PeopleResource.ListDirectoryPeopleRequest peopleRequest =
peopleService.People.ListDirectoryPeople();
peopleRequest.ReadMask = "names,emailAddresses,addresses,phoneNumbers";
peopleRequest.Sources = PeopleResource.ListDirectoryPeopleRequest.SourcesEnum.DIRECTORYSOURCETYPEDOMAINPROFILE;
peopleRequest.PageSize = 1000;
//peopleRequest.SortOrder = (PeopleResource.ConnectionsResource.ListRequest.SortOrderEnum)1;
var res = peopleRequest.Execute();
var list = peopleService.ContactGroups.List().Execute();
//var directoryPeople = peopleService.People.ListDirectoryPeople().Execute();
var person = res.People.FirstOrDefault();
foreach (var label in list.ContactGroups)
{
Console.WriteLine(label.ResourceName);
}
var all = list.ContactGroups.Where(s => s.Name== "myContacts").FirstOrDefault();
var members = peopleService.ContactGroups.Members.Modify(new ModifyContactGroupMembersRequest() { ResourceNamesToAdd = new List<string> { person.ResourceName }, ResourceNamesToRemove = new List<string> { } }, all.ResourceName).Execute();
我收到异常
{"Google.Apis.Requests.RequestError\r\nResource name \"people/XXXYYYY\" is not a valid contact person resource. [400]\r\nErrors [\r\n\tMessage[Resource name \"people/XXXYYYY\" is not a valid contact person resource.] Location[ - ] Reason[badRequest] Domain[global]\r\n]\r\n"}
这是否意味着我们无法使用目录中的人 api 添加用户作为联系人,如果是这样,最好的批量添加方法是什么?清除所有联系人并从目录中添加新用户?
问题是您正在尝试将配置文件添加到联系人组。您只能将联系人添加到联系人组。所以你应该只用个人资料电子邮件创建一个联系人。
这是我设置批量请求以创建联系人的函数。
将所有目录联系人转换为 Person,然后将其与 BatchRequest 一起发送。 最后执行批处理请求以创建联系人。
确保在 Person 中包含一个主要电子邮件地址。请注意,我检查了 ClientData - 这是我存储我的应用程序名称和 AD 的用户唯一 ID 以跟踪我的应用程序添加的内容的地方。
public Person CreateContact(BatchRequests batchRequest, Person contact, List<string> errors)
{
if (contact.EmailAddresses == null ||
contact.EmailAddresses.Count == 0 ||
!contact.EmailAddresses.Any(e => e.Metadata.Primary == true))
throw new Exception("Primary Email Address Not Found.");
try
{
if (!batchRequest.BatchCreateContactsRequest.Contacts.Any(c => c.ContactPerson.ClientData == contact.ClientData))
{
ContactToCreate contactToCreate = new ContactToCreate();
contactToCreate.ContactPerson = contact;
batchRequest.BatchCreateContactsRequest.Contacts.Add(contactToCreate);
}
}
catch (Exception ex)
{
// CurrentPersonEmail is the user's email whose contacts are being modified.
errors.Add($"Error;{CurrentPersonEmail};{ex.Message}. StackTrace:{ex.StackTrace}");
}
return contact;
}
准备好所有联系人后 - 然后执行请求(这不是完整的代码,但我希望你知道如何调用它):
BatchCreateContactsResponse batchCreateContactsResponse = PeopleService.People.BatchCreateContacts(BatchCreateContactsRequest).Execute();