40

Ok, so i've googled around, i've found threads here on stackoverflow and i've checked the official Facebook wiki and.. and what not..

I now hope that one of you guys sits on a Facebook API sample code for Python. This is what i've got so far and all i get is "Invalid Signature" via PyFacebook which appears to be a dead project:

from facebook import Facebook

api_key = '123456789______'
secret  = '<proper secret key>'
OTK = 'XXXXX' # <-- You get this from: https://www.facebook.com/code_gen.php?v=1.0&api_key=123456789______
long_term_key = None

fb = Facebook(api_key, secret)

def generate_session_from_onetime_code(fb, code):
    fb.auth_token = code
    return fb.auth.getSession()
if not long_term_key:
    long_term_key = generate_session_from_onetime_code(fb, OTK)['session_key']
    print 'Replace None with this in the .py file for long_term_key:'
    print long_term_key

fb.session_key = long_term_key
fb.uid = 000000001  # <-- Your user-id
fb.signature = api_key # <-- This doesn't work at all, MD5 of what?
#fb.validate_signature(fb) # <-- doesn't work either, prob need to pass MD5 handle?
print fb.friends.get() # <-- Generates "Invalid Signature"

"all" i want, is to retrieve my friends list for now, if there's a better API point me in the right direction but Facebook has officially declared their own Python SDK dead and pyfacebook is almost working for me but not quite..

So, please help.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 1
    Here's a tutorial that shows step by step how to use Python Social Auth to log in to Facebook, Google and Twitter: http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/ – orlenko Apr 18 '14 at 22:27
  • 1
    Try this out [fb 0.2.0.](https://pypi.python.org/pypi/fb/0.2.0) A light weight but very comprehensive facebook python sdk with detailed explanation of how tos – Samir Ahmed Aug 12 '13 at 21:57

1 Answers1

70

The unofficial fork of the python sdk is still working fine for me.

To retrieve your friends, generate an access token here: https://developers.facebook.com/tools/access_token/

Limitations:

  • A user access token with user_friends permission is required to view the current person's friends.
  • This will only return any friends who have used (via Facebook Login) the app making the request.
  • If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

Code

import facebook

token = 'your token'

graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")

friend_list = [friend['name'] for friend in friends['data']]

print friend_list
dom
  • 866
  • 8
  • 8
  • I used this code and facebook friends are fetched but not all friends are coming. most probably friends with no profile picture is not fetched. Why? – Pravitha V Feb 18 '13 at 10:51
  • 3
    It's called paged results, something they started with after this question was posted. Each search result is "paged", meaning you get 1-100 results out of 250, next fetch is 101-200, next is 201-250. – Torxed Mar 11 '13 at 18:36
  • Hello this is for friends. but if I need the people that like a page? Thank you very much – virtualsets Nov 03 '14 at 08:36
  • 4
    This must have changed as I only get `{'data': [], 'summary': {'total_count': 712}}` returned in `friend_list` – AfromanJ Mar 11 '15 at 14:04
  • The user_friends permission is probably missing from your app. I've updated the answer with the limitations. – dom Mar 12 '15 at 14:21
  • Passed a token from url which you gaved me.. right after this line in ipython `profile = graph.get_object("me")` i still got same problem: `An active access token must be used to query information about the current user` – holms Sep 09 '15 at 03:34
  • Note: *DO NOT* name your script `facebook.py` or it will not work. – 8bitjunkie Jun 10 '16 at 14:38
  • The script works. However, any facebook user needs to explicitly give permission to your account to get any data back. So when I run this script on my own account I get an empty array back. – Zuenie Feb 08 '17 at 08:56
  • Does not work for me.. :/ I get , 'module' object has no attribute 'GraphAPI' – Kostas Demiris Apr 21 '17 at 07:22
  • When I used a token that did not have the permissions set properly, I got an empty data field like @AfromanJ I used a different token that was set properly, and I got some data to return. However, it is only some of the data. There is a section with friend['paging'] that has cursors. I have a feeling that I am supposed to be using the cursors to traverse the data, but I am not quite sure how. – S. Miller Apr 11 '18 at 17:13