Search code examples
pythonflickr

Python Flickr-api Blank Response


I am trying to use the Python FlickrAPI package to manage some of my photos in Flickr. I am having difficulty getting certain functions to work properly. I start with the following:

import flickrapi
api_key='###MYkeyHERE###'
api_secret'####MYsecretHERE###'

flickr = flickrapi.FlickrAPI(api_key,secret=api_secret)

I also ran the token authentication and check on my Flickr account page to make sure it worked

(token, frob) = flickr.get_token_part_one(perms='write')
flickr.get_token_part_two((token, frob))

When I run the following to get a list of my sets it returns what appears to be an empty response.

photos = flickr.photos_search(user_id='MYuser@id', per_page='10')

The response simply looks like:

<Element 'rsp' at 0x22c2e10>

It has an attribute of 'stat' which is set to OK. I am wondering how to actually see the list of sets.

Funny thing is that the following code works perfectly and prints the names of the photos in one of my sets:

for photo in flickr.walk_set('72157636771398243'):
    print photo.get('title')

This leads me to think that my authentications are working properly, but that I am making some other error. I'd greatly appreciate help.


Solution

  • I don't think you're using it wrong. Look at the example response on the Flickr API flickr.photos.search documentation page.

    <photos page="2" pages="89" perpage="10" total="881">
        <photo id="2636" owner="47058503995@N01" 
                secret="a123456" server="2" title="test_04"
                ispublic="1" isfriend="0" isfamily="0" />
        <photo id="2635" owner="47058503995@N01"
                secret="b123456" server="2" title="test_03"
                ispublic="0" isfriend="1" isfamily="1" />
        <photo id="2633" owner="47058503995@N01"
                secret="c123456" server="2" title="test_01"
                ispublic="1" isfriend="0" isfamily="0" />
        <photo id="2610" owner="12037949754@N01"
                secret="d123456" server="2" title="00_tall"
                ispublic="1" isfriend="0" isfamily="0" />
    </photos>
    

    I tried this out out of curiosity and got these results, consistent with manipulating a parsed XML response string:

    >>> photos
    <Element 'rsp' at 0x2199a10>
    >>> photos[0]
    <Element 'photos' at 0x2199a30>
    >>> photos[0][0]
    <Element 'photo' at 0x2199cf0>
    >>> photos[0][0].get('id')
    '11124140143'
    >>> photos[0][0].get('owner')
    '62997566@N08'
    

    So I suspect that you are using the API properly, but you may need to experiment a bit to understand how best to use the results.

    According to the FlickrAPI documentation, the return value from FlickrAPI calls is an ElementTree. That should help figuring out how to tease apart the contents of the response:

    >>> import flickrapi
    >>> import xml
    >>> api_key = 'my API key'
    >>> api_secret = 'my API secret'
    >>> flickr = flickrapi.FlickrAPI(api_key,secret=api_secret)
    >>> r = flickr.photos_search(user_id='48439369@N00', per_page='10')
    >>> xml.etree.ElementTree.dump(r)
    <rsp stat="ok">
    <photos page="1" pages="376" perpage="10" total="3754">
        <photo farm="8" id="10550639536" isfamily="0" isfriend="0" ispublic="1" owner="48439369@N00" secret="fb5a1b526c" server="7336" title="Morgan and Quinn" />
        <photo farm="3" id="10550684964" isfamily="0" isfriend="0" ispublic="1" owner="48439369@N00" secret="0091541055" server="2840" title="Morgan and Quinn" />
        ...
    </photos>
    </rsp>
    >>> 
    

    The ElementTree class provides more methods for examining a result, iterating through the tree and finding nodes of interest.