XML parser syntax error

So, I am working with a code block that interacts with the Flickr API.

I get a "syntax error" in xml.parsers.expat.ExpatError (below). Now I can not understand how this will be a syntax error in the Python module.

I saw another similar SO question regarding the Wikipedia API, which seemed to return HTML instead of XML. The Flickr API returns XML; and I also get the same error when there should be no response from Flickr (e.g. flickr.galleries.addPhoto )

CODE:

 def _dopost(method, auth=False, **params): #uncomment to check you aren't killing the flickr server #print "***** do post %s" % method params = _prepare_params(params) url = '%s%s/%s' % (HOST, API, _get_auth_url_suffix(method, auth, params)) payload = 'api_key=%s&method=%s&%s'% \ (API_KEY, method, urlencode(params)) #another useful debug print statement #print url #print payload return _get_data(minidom.parse(urlopen(url, payload))) 

trace of TRACEBACK calls:

 Traceback (most recent call last): File "TESTING.py", line 30, in <module> flickr.galleries_create('test_title', 'test_descriptionn goes here.') File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1006, in galleries_create primary_photo_id=primary_photo_id) File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1066, in _dopost return _get_data(minidom.parse(urlopen(url, payload))) File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse return expatbuilder.parse(file) File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 928, in parse result = builder.parseFile(file) File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile parser.Parse(buffer, 0) xml.parsers.expat.ExpatError: syntax error: line 1, column 62 

(Code from http://code.google.com/p/flickrpy/ under the new BSD license)

UPDATE:

print urlopen(url, payload) == <addinfourl at 43340936 whose fp = <socket._fileobject object at 0x29400d0>>

Running urlopen(url, payload).read() returns HTML that is hard to read in the terminal: P, but I managed to parse "You are not logged in."
The weird part is that Flickr should not return anything here, or if permissions are a problem, it should return error 99: User not logged in / Insufficient permissions , as is the case with the GET function (which I expected would be in valid XML).

I connected to Flickr (in the browser) and the program was correctly authenticated with delete permissions (dangerous, but I wanted to avoid permission problems.)

+4
source share
2 answers

This seems to fix my problem:

 url = '%s%s/?api_key=%s&method=%s&%s'% \ (HOST, API, API_KEY, method, _get_auth_url_suffix(method, auth, params)) payload = '%s' % (urlencode(params)) 

It seems that the key and API method should have been in the url and not in the payload. (Or maybe only one should be there, but it works anyway :-)

+2
source

SyntaxError usually means an error in the Python syntax, but I think expatbuilder is overloading it, which means an XML syntax error. Put a try:except block around it and print out the contents of payload and find out what happened with the first line.

My guess is that flickr rejects your request for some reason and returns a plain text error message that has an invalid xml character in column 62, but that could be any number of things. You probably want to check the http status code before parsing it.

Also, this is a little strange, this method is called _dopost , but you seem to really send the http GET . Perhaps why this fails.

+6
source

All Articles