Listing contacts in System groups

192 views
Skip to first unread message

Karra

unread,
Apr 22, 2012, 8:58:08 PM4/22/12
to Google Contacts, Shared Contacts and User Profiles APIs

When I try to run the sample code from the most recent python library
for gdata, I am unable to list the contacts in my System Groups. I
separately tried to fetch the group feed for 'My Contacts' which
exited with a similar error. I am pasting below the output from
running the sample script. The Group ID of My Contacts is:
http://www.google.com/m8/feeds/groups/karra.etc%40gmail.com/base/6

Any idea what I am doing wrong?

---
Contacts Sample
1) List all of your contacts.
2) Create a contact.
3) Query contacts on updated time.
4) Modify a contact.
5) Delete a contact.
6) List all of your contact groups.
7) Query your groups on updated time.
8) Exit.

> 1
Traceback (most recent call last):
File "./contacts_example.py", line 352, in <module>
main()
File "./contacts_example.py", line 348, in main
sample.Run()
File "./contacts_example.py", line 294, in Run
self.ListAllContacts()
File "./contacts_example.py", line 136, in ListAllContacts
feed = self.gd_client.GetContacts()
File "/Users/sriramkarra/Downloads/software/unix/gdata-2.0.17/src/
gdata/contacts/client.py", line 201, in get_contacts
desired_class=desired_class, **kwargs)
File "/Users/sriramkarra/Downloads/software/unix/gdata-2.0.17/src/
gdata/client.py", line 640, in get_feed
**kwargs)
File "/Users/sriramkarra/Downloads/software/unix/gdata-2.0.17/src/
gdata/client.py", line 278, in request
version=get_xml_version(self.api_version))
File "/Users/sriramkarra/Downloads/software/unix/gdata-2.0.17/src/
atom/core.py", line 520, in parse
tree = ElementTree.fromstring(xml_string)
File "<string>", line 106, in XML
cElementTree.ParseError: unbound prefix: line 5, column 1552

Karra

unread,
Apr 23, 2012, 2:33:49 AM4/23/12
to google-co...@googlegroups.com

On Monday, 23 April 2012 06:28:08 UTC+5:30, Karra wrote:
 
cElementTree.ParseError: unbound prefix: line 5, column 1552

I just found this thread that suggests there is an uncaught parsing error in the internal google code. Can someone from the Contacts team comment on this, please?


-Sriram

Karra

unread,
Apr 24, 2012, 3:37:48 AM4/24/12
to google-co...@googlegroups.com
I guess this issue has been known since Aug 2011, but there has been no response from Google. But thank goodness, the code is open... I am able to apply the attached patch and move on. Posting here so others can benefit as well.

As noted in the comments, I have no idea why the invalid XML is being generated, and hence, what the real impact of the patch is. I guess I'll find out soon...

------

    Fix invalid contact entry XML from Google Data server
    
    Certain contacts - it is not entirely clear what sort of contacts -
    generate invliad XML from Google that the client libraries are
    unable to parse. This can cause an irrecoverable exception that looks
    like: "cElementTree.ParseError: unbound prefix: line 1, column 3089"
    
    The actual column number and line number may differ. We fix the problem
    by patching the incoming XML by editing out the invalid parts. This p
    atch is not extensively tested, but certainly allows life to move
    forward.

diff --git a/lib/atom/core.py b/lib/atom/core.py
index a19a70d..1da703a 100644
--- a/lib/atom/core.py
+++ b/lib/atom/core.py
@@ -21,6 +21,7 @@
 __author__ = 'j...@google.com (Jeff Scudder)'
 
 
+import string
 import inspect
 try:
   from xml.etree import cElementTree as ElementTree
@@ -517,6 +518,19 @@ def parse(xml_string, target_class=None, version=1, encoding=None):
       xml_string = xml_string.encode(STRING_ENCODING)
     else:
       xml_string = xml_string.encode(encoding)
+
+  # For some contacts, the google data protocol returns invalid XML. This
+  # issue was originally reported on the python client api google groups, but
+  # it essentially an underlying data api error. More on this error at:
+  #
+  # This is a fatal error and applications cannot proceed when this error
+  # occurs. The following hack patches the received xml and allows life to
+  # go on. Ofcourse, there is really no confirmation yet from Google on why we
+  # are getting invalid XML for certain contacts in the first place.
+  xml_string = string.replace(xml_string, r'<ns0:cc>', '')
+  xml_string = string.replace(xml_string, r'</ns0:cc>', '')
+
   tree = ElementTree.fromstring(xml_string)
   return _xml_element_from_tree(tree, target_class, version)
 
Reply all
Reply to author
Forward
0 new messages