如何通过Facebook-sdk python api获取用户的帖子?

如何通过Facebook-sdk python api获取用户的帖子?

问题描述:

我使用Facebook-jssdk授权我的应用程序读取用户个人资料和用户帖子。

I use facebook-jssdk to authorize my application for read access to user profile and user posts.

FB.login(
    function(response) { },  
    {scope:'user_status,user_likes,user_photos,user_videos,user_questions,read_stream,user_posts'}
);

然后我尝试接收用户的帖子。
import json
from time import mktime
from urllib import urlopen
from facebook import GraphAPI
from datetime import datetime

Then I'm trying to receive user posts. import json from time import mktime from urllib import urlopen from facebook import GraphAPI from datetime import datetime

ga = GraphAPI()
access_token = ga.get_app_access_token(settings.FACEBOOK_APP_ID,     settings.FACEBOOK_APP_SECRET)

url = "https://graph.facebook.com/fql?q=SELECT+created_time+FROM+stream+ WHERE+source_id=%s+AND+created_time<%d&access_token=%s" % (
social_id,   # user id in facebook, taken from facebook-jssdk
int(mktime((datetime.now().timetuple()))),
access_token
)
data = json.loads(urlopen(url).read())
logger.debug(data)
# {u'data': []}


ga = GraphAPI(access_token)
ga.get_object('/508708815952422/posts/', token=token)
# {u'data': []}

数据总是空的...我做错了什么?

Data is always empty... What am I doing wrong?

有用的信息
自Facebook登录v2 .5权限要求审核正常工作。
https://developers.facebook.com/docs/facebook-login/permissions/v2.2

你想知道如何使用python api获取用户的帖子吧?

You want to know how to get user posts using a python api, right?

我正在使用 facebook-sdk 在django项目中,我得到它的工作,像这样(Implementation - services / facebook.py):

I'm using facebook-sdk within a django project and I got it to work, like this (Implementation - services/facebook.py):

from django.conf import settings
import facebook
import requests

class FacebookFeed:
    token_url = 'https://graph.facebook.com/oauth/access_token'
    params = dict(client_id=settings.SOCIAL_AUTH_FACEBOOK_KEY, client_secret=settings.SOCIAL_AUTH_FACEBOOK_SECRET,
                  grant_type='client_credentials')

    @classmethod
    def get_posts(cls, user, count=6):
        try:
            token_response = requests.get(url=cls.token_url, params=cls.params)
            access_token = token_response.text.split('=')[1]
            graph = facebook.GraphAPI(access_token)
            profile = graph.get_object(user)
            query_string = 'posts?limit={0}'.format(count)
            posts = graph.get_connections(profile['id'], query_string)
            return posts
        except facebook.GraphAPIError:
            return None

注意:在我的情况下,如果您将用户登录到您的应用程序并已具有令牌,则需要使用客户端凭据流来使用密钥和密码设置来获取访问令牌在你身边,然后忽略这些行:

Note: In my case I need to fetch the access token using the client-credentials flow, making use of the Key and Secret settings, if you're logging users into an app of yours and already have tokens on your side, then ignore the lines:

token_response = requests.get(url=cls.token_url, params=cls.params)
access_token = token_response.text.split('=')[1]

(views.py):

Usage (views.py):

from django.http import HttpResponse
from app.services.social_networks.facebook import FacebookFeed

def get_facebook_posts(request, user):
    posts = FacebookFeed.get_posts(user=user)
    if not posts:
        return HttpResponse(status=500, content="Can't fetch posts for desired user", content_type="application/json")
    return HttpResponse(json.dumps(posts), content_type="application/json")

希望这有帮助,任何问题,请问ask =)

Hope this helps, any problem, please do ask =)