且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python - Facebook API - 需要一个工作示例

更新时间:2022-10-14 22:38:55

python sdk 的非官方分支仍然存在对我来说很好.

要检索您的朋友,请在此处生成访问令牌:https://developers.facebook.com/tools/access_token/

限制:

  • 需要具有 user_friends 权限的用户访问令牌才能查看当前人的朋友.
  • 这只会返回使用(通过 Facebook 登录)发出请求的应用的所有朋友.
  • 如果此人的好友拒绝了 user_friends 权限,该好友将不会出现在此人的好友列表中.

代码

导入脸书令牌 = '你的令牌'图 = facebook.GraphAPI(令牌)profile = graph.get_object("我")朋友 = graph.get_connections("我","朋友")friend_list = [friend['name'] 为朋友中的朋友['data']]打印朋友列表

Ok, so i've googled around, i've found threads here on *** 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.

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