4

So I'm working with a block of code which communicates with the Flickr API.

I'm getting a 'syntax error' in xml.parsers.expat.ExpatError (below). Now I can't figure out how it'd be a syntax error in a Python module.

I saw another similar question on SO regarding the Wikipedia API which seemed to return HTML intead of XML. Flickr API returns XML; and I'm also getting the same error when there shouldn't be a response from Flickr (such as 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)))

TRACEBACK:

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 New BSD licence)

UPDATE:

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

Doing a urlopen(url, payload).read() returns HTML which is hard to read in a terminal :P but I managed to make out a 'You are not signed in.'
The strange part is that Flickr shouldn't return anything here, or if permissions are a problem, it should return a 99: User not logged in / Insufficient permissions error as it does with the GET function (which I'd expect would be in valid XML).

I'm signed in to Flickr (in the browser) and the program is properly authenticated with delete permissions (dangerous, but I wanted to avoid permission problems.)

2 Answers 2

6

SyntaxError normally means an error in Python syntax, but I think here that expatbuilder is overloading it to mean an XML syntax error. Put a try:except block around it, and print out the contents of payload and to work out what's wrong with the first line of it.

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

Also, it's a bit strange this method is called _dopost but you seem to actually be sending an http GET. Perhaps that's why it's failing.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure exactly what's going on here. I only wanted to add some more API calls, and I hit this issue when testing. There's another function called _doget() which seems to work slightly differently (and correctly :-). Thanks for the idea to print the payload. My first bug, so I'm still learning :D
Reading Python docs, it seems that if you have a data parameter to the urlopen() function, it sents a POST request, not a GET request. So that part's all good (it seems.)
It's not raising a SyntaxError, it's raising an ExpatError.
2

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 API key and method had to be in the URL not in the payload. (Or maybe only one needed to be there, but anyways, it works :-)

2 Comments

So was my guess right that it was giving you a non-xml error?
Wasn't really an error. The API_Key wasn't in the url so it must've not realized it's an API call so it assumed it was a browser and responded with a web page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.