0
votes

Google Contacts API returns list of email addresses I sent to, but not those in my Contacts list?

I'm using google contact api and try to get the list of emails in my gmail contacts. The google contact is at https://contacts.google.com/u/0/preview/all .

Below it is the code I use:

public List<GMail> GetContacts(GooglePlusAccessToken serStatus)
{
    List<GMail> ret = new List<GMail>();

    string google_client_id = _ClientId;
    string google_client_sceret = _ClientSeceret;

    string refreshToken = serStatus.refresh_token;
    string accessToken = serStatus.access_token;
    string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";
    OAuth2Parameters oAuthparameters = new OAuth2Parameters()
    {
        ClientId = google_client_id,
        ClientSecret = google_client_sceret,
        RedirectUri = GetRedirectUrl(), 
        Scope = scopes,
        AccessToken = accessToken,
        RefreshToken = refreshToken
    };

    RequestSettings settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
    ContactsRequest cr = new ContactsRequest(settings);
    ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
    query.NumberToRetrieve = _NumberToRetrieve;
    Feed<Contact> feed = cr.Get<Contact>(query);

    foreach (Contact entry in feed.Entries)
    {
        foreach (EMail email in entry.Emails)
        {
            GMail g = new GMail(email.Address);
            ret.Add(g);
        }
    }

    return ret;
}

But for some reasons, the code returns the list of email addresses that I sent to in the past, but not the emails in my contact list. Is this correct? Anyone knows why?

Thanks

** Thanks for Allen's answer, here is my code, and it works great! The contact group is actually: SystemGroup:[email protected]**

public List<GMail> GetContacts_2(GooglePlusAccessToken serStatus)
{
    List<GMail> ret = new List<GMail>();

    string google_client_id = _ClientId;
    string google_client_sceret = _ClientSeceret;
    /*Get Google Contacts From Access Token and Refresh Token*/
    string refreshToken = serStatus.refresh_token;
    string accessToken = serStatus.access_token;
    string scopes = "https://www.google.com/m8/feeds/groups/default/full/?v=3.0";

    OAuth2Parameters oAuthparameters = new OAuth2Parameters()
    {
        ClientId = google_client_id,
        ClientSecret = google_client_sceret,
        RedirectUri = GetRedirectUrl(), 
        Scope = scopes,
        AccessToken = accessToken,
        RefreshToken = refreshToken
    };


    RequestSettings settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
    ContactsRequest cr = new ContactsRequest(settings);
    GroupsQuery query = new GroupsQuery(GroupsQuery.CreateGroupsUri("default"));
    query.NumberToRetrieve = 1000;  // _NumberToRetrieve; // 100;// 5000;
    //Feed feed = cr.Get(query);
    Feed<Group> feed = cr.Get<Group>(query);
    string gpId = string.Empty;
    foreach (Group gp in feed.Entries)
    {
        if (gp.Title.Contains("Contacts"))
        {
            gpId = gp.Id;
            //break;
        }

        //// for testing
        //GMail g = new GMail(gp.Title.Replace(" ", "") + "@gmail.com");
        //ret.Add(g);
    }

    if (gpId.Length > 0)
    {
        scopes = "https://www.google.com/m8/feeds/contacts/default/full/?v=3.0";

        oAuthparameters = new OAuth2Parameters()
        {
            ClientId = google_client_id,
            ClientSecret = google_client_sceret,
            RedirectUri = GetRedirectUrl(),
            Scope = scopes,
            AccessToken = accessToken,
            RefreshToken = refreshToken
        };

        settings = new RequestSettings("<var>Invite GMail Friends</var>", oAuthparameters);
        //ContactsRequest cr = new ContactsRequest(settings);
        cr = new ContactsRequest(settings);
        ContactsQuery query2 = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
        query2.NumberToRetrieve = _NumberToRetrieve; // 100;// 5000;
        query2.OrderBy = ContactsQuery.OrderByLastModified;
        query2.SortOrder = ContactsQuery.SortOrderDescending;
        query2.Group = gpId;
        Feed<Contact> feed2 = cr.Get<Contact>(query2);

        foreach (Contact entry in feed2.Entries)
        {
            foreach (EMail email in entry.Emails)
            {
                GMail g = new GMail(email.Address);
                ret.Add(g);
            }
        }

        //if (ret.Count <= 0)
        //{
        //    GMail g = new GMail("[email protected]");
        //    ret.Add(g);
        //}

    }
    //else
    //{
    //    // for testing
    //    GMail g = new GMail("[email protected]");
    //    ret.Add(g);
    //}

    return ret;
}
1

1 Answers

2
votes

To start with, I feel your pain. Google's APIs are the worst. Period.

The way I got around it (not an efficient way of doing things but this is the best I could find. Else Google returns all those bogus contact emails that were ever communicated with the account holder, could be thousands of such entries in a typical GMail account)) is this (its VB.net code but you can get a gist and convert easily)

FIRST RETRIEVE CONTACT GROUPS AND LOOK FOR THE TITLE "CONTACTS"

Dim settings As New RequestSettings("MyApp", RefreshOAuthToken())
Dim cr As New ContactsRequest(settings)
    'retrieve Contacts group (this is to retrieve only real contacts)
Dim groupquery As New GroupsQuery(GroupsQuery.CreateGroupsUri("default"))
Dim fgrp As Feed(Of Group) = cr.Get(Of Group)(groupquery)
Dim GroupAtomId As String = ""
For Each gr In fgrp.Entries
    If gr.Title.Contains("Contacts") Then
       GroupAtomId = gr.Id
             Exit For
    End If
Next

THEN USE THE ID OF THE CONTACT GROUP TO SEARCH IN CONTACT

Dim query As New ContactsQuery(ContactsQuery.CreateContactsUri("default"))
query.NumberToRetrieve = 2000
query.OrderBy = ContactsQuery.OrderByLastModified
query.SortOrder = ContactsQuery.SortOrderDescending
query.Group = GroupAtomId

Dim f As Feed(Of Contact) = cr.Get(Of Contact)(query)

For Each entry As Contact In f.Entries
 'Do something with the data, these are real contacts in GMail
Next

So the point here is to first get the group id and use it in contact search. I believe this is what your code is missing.