Reputation: 1
I'm working on an app in Flask and want to incorporate Facebook profile pictures for users. Since I have the users' Facebook IDs, I found a really easy way to access the photos through Facebook Graph, which returns JSON data at this URL:
img_url = 'http://graph.facebook.com/%s/picture?width=%d&height=%d&redirect=false' \
% (fb_id, width, height)
Here's the JSON data that img_url should return:
{
"data": {
"height": 200,
"is_silhouette": false,
"url": "JPG_URL_HERE",
"width": 200
}
}
For the specific image URL, I'm using Requests to get a Python dictionary with unicode objects.
r = requests.get(img_url).json()
return r['data']['url'].encode('utf-8')
All of it works fine in the Python interpreter and returns a string with the image URL. When I run the app with the code in Vagrant, though, I get a KeyError for 'data'. Any thoughts on what's going wrong?
File "/vagrant/app/templates/user/profile.html", line 1, in top-level template code
{% extends "base.html" %}
File "/vagrant/app/templates/base.html", line 38, in top-level template code
{% block content %}{% endblock %}
File "/vagrant/app/templates/user/profile.html", line 9, in block "content"
<p><img src="{{ current_user.picture(200, 200) }}"></p><br>
File "/vagrant/app/models.py", line 52, in picture
return r['data']['url'].encode('utf-8')
KeyError: 'data'
Upvotes: 0
Views: 1678
Reputation: 12716
In your codes:
r = requests.get(img_url).json()`
I think the r
may be result in type str
, so you should translate it into dict like this:
import json
r = requests.get(img_url).json()
r_dict = json.loads(r)
return r_dict['data']['url'].encode('utf-8')
try it.
Upvotes: 0