Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active August 29, 2015 13:57
Show Gist options
  • Save cansadadeserfeliz/9915903 to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/9915903 to your computer and use it in GitHub Desktop.
Python, Facebook, python-social-auth: get a list of Facebook friends with their name, location, profile picture and url to profile page
SOCIAL_AUTH_FACEBOOK_SCOPE = [
'email',
'user_friends',
'friends_location',
]
class FlightDetail(View):
# ...
def get_context_data(self, **kwargs):
context = super(FlightDetail, self).get_context_data(**kwargs)
friends_in_city = []
if request.user.is_authenticated():
social_user = request.user.social_auth.filter(
provider='facebook',
).first()
if social_user:
url = u'https://graph.facebook.com/{0}/' \
u'friends?fields=id,name,location,picture' \
u'&access_token={1}'.format(
social_user.uid,
social_user.extra_data['access_token'],
)
request = urllib2.Request(url)
friends = json.loads(urllib2.urlopen(request).read()).get('data')
for friend in friends:
if (
friend.get('location')
and flight.arrival_airport.city
and flight.arrival_airport.city.name
in friend['location']['name']
):
friends_in_city.append({
'name': friend['name'],
'photo': friend['picture']['data']['url'],
'profile_url':
'https://www.facebook.com/{0}'.format(friend['id']),
})
context.update({
'friends_in_city': friends_in_city,
})
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment