Problems adding a new contact with Python

456 views
Skip to first unread message

Greg Broiles

unread,
Feb 11, 2012, 5:31:23 AM2/11/12
to google-co...@googlegroups.com
Here's some example code:

import atom
import gdata.contacts
import gdata.contacts.service
import gdata.contacts.data
import gdata.data

gd_client = gdata.contacts.service.ContactsService()
gd_client.email = 'MYEMAIL'
gd_client.password = 'MYPASSWORD'
gd_client.source = 'test'
gd_client.ProgrammaticLogin()

name="Test Person"
primary_email="teste...@example.com"

new_contact = gdata.contacts.ContactEntry(title=atom.Title(text=name))
new_contact.email.append(gdata.contacts.Email(address=primary_email, primary='true', rel=gdata.contacts.REL_WORK))
new_contact.phone_number.append(gdata.contacts.PhoneNumber("650-555-5555", primary='true'))
new_contact.structured_postal_address.append(gdata.data.StructuredPostalAddress(rel=gdata.data.WORK_REL, primary='true',
    street=gdata.data.Street(text='1600 Amphitheatre Pkwy'),
    city=gdata.data.City(text='Mountain View'),
    region=gdata.data.Region(text='CA'),
    postcode=gdata.data.Postcode(text='94043'),
    country=gdata.data.Country(text='United States')))

contact_entry = gd_client.CreateContact(new_contact)

--

and this is the error it gives me:

C:\Python27\python.exe C:/Users/gbroiles/Projects/google-contacts/c4.py
Traceback (most recent call last):
  File "C:/Users/gbroiles/Projects/google-contacts/c4.py", line 26, in <module>
    contact_entry = gd_client.CreateContact(new_contact)
  File "C:\Python27\lib\site-packages\gdata\contacts\service.py", line 132, in CreateContact
    converter=gdata.contacts.ContactEntryFromString)
  File "C:\Python27\lib\site-packages\gdata\service.py", line 1236, in Post
    media_source=media_source, converter=converter)
  File "C:\Python27\lib\site-packages\gdata\service.py", line 1322, in PostOrPut
    headers=extra_headers, url_params=url_params)
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 93, in optional_warn_function
    return f(*args, **kwargs)
  File "C:\Python27\lib\site-packages\atom\service.py", line 176, in request
    content_length = CalculateDataLength(data)
  File "C:\Python27\lib\site-packages\atom\service.py", line 736, in CalculateDataLength
    return len(str(data))
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 377, in __str__
    return self.ToString()
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 374, in ToString
    return ElementTree.tostring(self._ToElementTree(), encoding=string_encoding)
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 369, in _ToElementTree
    self._AddMembersToElementTree(new_tree)
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 329, in _AddMembersToElementTree
    instance._BecomeChildElement(tree)
AttributeError: 'StructuredPostalAddress' object has no attribute '_BecomeChildElement'

--

the problem seems to be in the StructuredPostalAddress part, but I can't seem to figure out what it doesn't like. The example runs fine if I comment out the address lines - but I would like to store addresses for my contacts, surprisingly enough. 

Any ideas?

Greg Broiles

unread,
Feb 11, 2012, 5:34:54 AM2/11/12
to google-co...@googlegroups.com
the phone number line should be

new_contact.phone_number.append(gdata.contacts.PhoneNumber(text="650-555-5555", primary='true', rel=gdata.contacts.REL_WORK))


Alain Vongsouvanh

unread,
Feb 23, 2012, 6:05:11 PM2/23/12
to google-co...@googlegroups.com
Hello,

Creating contacts with the Python client library is documented here:

Best,
Alain

On Sat, Feb 11, 2012 at 2:34 AM, Greg Broiles <gbro...@gmail.com> wrote:
the phone number line should be

new_contact.phone_number.append(gdata.contacts.PhoneNumber(text="650-555-5555", primary='true', rel=gdata.contacts.REL_WORK))


--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh | Developer Programs Engineer
Reply all
Reply to author
Forward
0 new messages