1
votes

I am trying top Get all contacts from a Google account using the .Net API.

Case 1:

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

When I do something like the previous code segment it brings me all the contacts but it also brings the Other Contacts for the same user. This can be very costly in terms of performance. I then filter locally the contacts and keep only the ones that I am interested in but the damage is already done.

Case 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());
}

In this case iterate through the Groups in the account and get the contacts for each group. Other Contacts are not a group, so I manage to escape them. By default when a user creates a contact in whatever Group, the contact is added to this Group and to the "Master Group" which is My Contacts. So a contact may be part of one (My Contacts - if created there) or more groups (if contact created elsewhere). However, the user can manually remove the contact from My Contacts and keep it only in another Group.

  1. If I iterate through all groups and then filter locally I will fetch all contacts but most probably more than one time => poor performance.
  2. If I get all contacts from My Contacts I risk of missing some of them.

So, the optimal solution to the above would be to fetch all contacts excluding Other Contacts. Could someone propose a solution (maybe add some parameters to the ContactsQuery or something)?

Ref: Google Contacts API v3.0

1
I'm not aware of any other methods beyond what you've already outlined.Eric Koleda

1 Answers

0
votes

If you only want your contacts, I would recommend using the Google People API, which is different that the Google Contacts API. This is available as a Nuget package. The Google People API currently does not allow you to get your "Other Contacts", so their normal GET methods will work to retrieve just the entries in your contacts. Here is an example:

// 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));