Recommendation for getting user avatar
I don't see a method to get a user's avatar/main profile photo. And it's not returned in get_myself().
I also tried creating a new method in pyfb.py to accomplish this...
def get_avatar(self, id=None):
if id is None:
id = "me"
return self._client.get_one(id, "Picture")
... but that always returns the same user object returned in get_myself().
I feel like there has to be a simple solution I'm overlooking here. Suggestions/thoughts?
PS: I based "Picture" on Facebook's documentation for how to get a profile photo via the Graph API here: https://developers.facebook.com/docs/reference/api/using-pictures/
You can render the current profile picture for any object that has a picture associated with it. All you have to do is add the suffix /picture to the object URL. For example, this will render a public profile picture:
I figured out the problem: while get_one is conceptually supposed to retrieve one object, and it does when called from get_list — obj = self.get_one(path, object_name) — it doesn't when called on its own. This is because (and I don't know why) get_list is used in a weird way right now. It's only called by four different methods in pyfb.py and each only ever passes 2 of its 3 ideal arguments: id and path, but never object_type.
So there's an if statement built into get_list that sets object_type to path's value, so it can validly be passed to get_one, and then combines the two objects' values into path (yikes):
if object_name is None:
object_name = path
path = "%s/%s" % (id, path.lower())
The crux of the problem here is that, because get_list behaves like this, get_one has gotten away with not ever having to be passed object_name of its own volition because it gets it by proxy in path. get_one hasn't suffered from this because the only helper functions that use it directly (i.e., not via get_list) are get_myself() and get_user_by_id() (which I imagine no one is really using given get_myself() almost always makes more sense and, well, no one else has opened this ticket).
I added similar logic to get_one to bypass this:
path = "%s/%s" % (path, object_name.lower())
So it looks like:
def get_one(self, path, object_name):
"""
Gets one object
"""
path = "%s/%s" % (path, object_name.lower())
data = self._make_auth_request(path)
obj = self._make_object(object_name, data)
if hasattr(obj, 'error'):
raise PyfbException(obj.error.message)
return obj
And, while that does allow me to get single objects of any type now, it still doesn't fix the avatar problem. Because the avatar returned from facebook.com/user/picture is an image, not a URL. You have to pass ?redirect=false to get a JSON blob with a URL.
tl;dr: Either get_one needs kwarg support or there needs to be a fairly special-cased one-off for getting user avatars. Because there's no way other way to get them right now from what I see.