Search code examples
vb.netgdatagdata-apigoogle-contacts-api

Adding a Birthday to a Contact to Google Contacts


I'm a bit stumped on how I should go about adding a birthday to a contact entry in Google Contacts API in .NET.

Right now, I am creating a new contact entry:

Dim contact As New Contact
contact.Name = New Name()
...
contact.ContactEntry.Birthday = pushedBirthday
...
service.Insert(feedUri, contact) 'Insert using the Contacts API

Right now everytime I do this, I get an HTTP 400 (Bad request) from Google. I'm not sure what is going on here... the examples shown in the Contacts API docs don't really go into detail in terms of creating Birthdays, so as a result I'm a bit lost.

Any ideas?


Solution

  • Woohoo! I figured this out. Apparently, it was the formatting of my date for the birthday that was throwing an error because it was invalid schema on Google's end.

    Here's how I found out:

    In my code, I basically extracted the Atom XML entry to view the contact object as an XML like this:

                Dim st As New System.IO.MemoryStream()
    
                newContact.AtomEntry.SaveToXml(st)
    
                Dim sw As New System.IO.StreamWriter("C:\test.xml")
                sw.Write(Encoding.UTF8.GetString(st.ToArray()))
                sw.Close()
    

    This basically wrote the XML representation to disk as test.xml. Now, I took a look at the XML and the birthday was there. So I went over to Google's OAuth2 playground here: https://developers.google.com/oauthplayground/ and passed in the XML message using the same POST CreateContact operation.

    As it turns out, the date validation was expecting data type of xs:date. My date format was MM/dd/yyyy which was failing. Instead, I changed it to yyyy-MM-dd and things worked!

    I was not aware of the the OAuth2 playground, but it is extremely useful in getting a more detailed exception when errors occur. :)