【问题标题】:Get All Contacts from Google account Excluding Other Contacts using the .Net api使用 .Net api 从 Google 帐户获取所有联系人,不包括其他联系人
【发布时间】:2014-10-24 09:27:45
【问题描述】:

我正在尝试使用 .Net API 从 Google 帐户获取所有联系人。

案例一:

var cr = new ContactsRequest(settings);
var feed = cr.GetContacts();

当我执行与前面的代码段类似的操作时,它会为我带来所有联系人,但也会为同一用户带来其他联系人。这在性能方面可能非常昂贵。然后我在本地过滤联系人 只保留我感兴趣但损坏已经造成的那些。

案例 2:

var cr = new ContactsRequest(settings);
var groupFeed = cr.GetGroups();
foreach (var group in groupFeed.Entries.ToList()) {
    var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default")) {Group = group.Id};
    var contactFeed = cr.Get<Contact>(query);
    contactsList.AddRange(contactFeed.Entries.ToList());
}

在这种情况下,遍历帐户中的组并获取每个组的联系人。其他联系人不是一个组,所以我设法逃脱了他们。默认情况下,当用户在任何组中创建联系人时,联系人将添加到此组和“主组”,即我的联系人。所以一个联系人可能是一个(我的联系人 - 如果在那里创建)或多个组(如果在其他地方创建联系人)的一部分。 但是,用户可以手动从“我的联系人”中删除该联系人,并将其仅保留在另一个组中。

  1. 如果我遍历所有组,然后在本地过滤,我将获取所有联系人,但很可能不止一次 => 性能不佳。
  2. 如果我从“我的联系人”中获取所有联系人,我可能会丢失其中的一些。

因此,上述问题的最佳解决方案是获取除其他联系人之外的所有联系人。有人可以提出一个解决方案吗(也许在 ContactsQuery 中添加一些参数或其他东西)?

参考:Google Contacts API v3.0

【问题讨论】:

  • 我不知道除了您已经概述的任何其他方法。

标签: .net google-contacts-api


【解决方案1】:

如果您只需要联系人,我建议您使用 Google People API,它与 Google Contacts API 不同。这可作为 Nuget 包提供。 Google People API 当前不允许您获取“其他联系人”,因此他们的常规 GET 方法将仅用于检索您的联系人中的条目。这是一个例子:

// Setup authentication
var credential = new UserCredential(flow, "<LOGGED IN USER's EMAIL ADDRESS>", token);
var service = new PeopleService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = _applicationName,
        });

var connectionsRequest = service.People.Connections.List("people/me");

// Assign what properties you want filled in. Fewer properties will be more performant.
connectionsRequest.RequestMaskIncludeField = "person.names,person.emailAddresses,person.photos";

do
{
    var connectionsResponse = connectionsRequest.Execute();
    var connections = connectionsResponse.Connections;

    foreach (var connection in connections)
    {
        // TODO: Handle each record as you need
    }

    // This API pages results and you manually need to retrieve the next set
    connectionsRequest.PageToken = connectionsResponse.NextPageToken;
} while (!string.IsNullOrEmpty(connectionsRequest.PageToken));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多