Question

I got this code to "work" (read not throwing an exception). But the contact is not added to my gmail contacts as it should (nor on my android phone which sync contacts).

Note that I can read the contacts correctly so the credentials are right.

I read that I should check for a Status on the request, but the only Status I see is a property of ContactEntry and it's always null.

This is a console app for tests.

public static void AddContact(ContactDetail contact)
        {
            GContactService = new ContactsService("Contact Infomation");
            GContactService.setUserCredentials("myemail@gmail.com", "mypassword");

            ContactEntry newEntry = new ContactEntry();
            newEntry.Title.Text = contact.Name;
            newEntry.Name = new Name() { FullName = "Tristan Savage", GivenName = "Tristan", FamilyName = "Savage"};

            EMail primaryEmail = new EMail(contact.EmailAddress1);
            primaryEmail.Primary = true;
            primaryEmail.Rel = ContactsRelationships.IsWork;
            newEntry.Emails.Add(primaryEmail);

            PhoneNumber phoneNumber = new PhoneNumber(contact.Phone);
            phoneNumber.Primary = true;
            phoneNumber.Rel = ContactsRelationships.IsMobile;
            newEntry.Phonenumbers.Add(phoneNumber);

            PostalAddress postalAddress = new PostalAddress();
            postalAddress.Value = contact.Address;
            postalAddress.Primary = true;
            postalAddress.Rel = ContactsRelationships.IsCompanyMain;
            newEntry.PostalAddresses.Add(new StructuredPostalAddress() { City = "montreal", Label = "Bureau"});

            newEntry.Content.Content = contact.Details;

            Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default")); //default

            ContactEntry createdEntry = (ContactEntry)GContactService.Insert(feedUri, newEntry); 

        }
Was it helpful?

Solution

I finally figured it out. The GroupMembership is required in order for the contact to get to the mobile device!

Here is the missing part:

var groupMembership = new GroupMembership
{
    HRef = "http://www.google.com/m8/feeds/groups/" + utilisateur.CourrielGmailContacts + "/base/6"
};
newEntry.GroupMembership.Add(groupMembership);

OTHER TIPS

Try commenting some lines. With only the name and email, you should be able to create your contact.

I'm able to create a contact with the .NET example here: https://developers.google.com/google-apps/contacts/v3/?hl=fr#creating_contacts

with Request initialized this way: ContactsRequest Request = new ContactsRequest(new RequestSettings("appName", "user@gmail.com", "password"));

Please note that your new contact is not associated to a group. So you won't see the contact appearing in "My Contacts"/"Mes Contacts". You should see it in "Other contacts"/"Autres contacts".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top