Search code examples
djangofacebookfacebook-graph-apifacebook-opengraphfandjango

How should I get Open Graph JSON object to pass in facepy class


I am trying to configure Open Graph in my app such that, when ever a person click a "link" in it, the app should "post" it on his feeds/ timeline/ activity. (I created open graph actions for these features and successfully added meta tags in the page).

So, I am using fandjango and facepy to do this job.

This is how my code looks..

from facepy import GraphAPI

@csrf_exempt
@facebook_authorization_required(permissions=["publish_actions"])
def ViewPage (request):
    access_token = request.facebook.user.oauth_token
    profile_id = request.facebook.user.facebook_id
    path = "/%d/feed" %profile_id

    # How should I get the OpenGraph JSON obj
    og_data = ??
    graph = GraphAPI(access_token)
    graph.post(path, og_data)
    ...
    return render_to_response("view.html", context)

How should I get the open graph JSON obj to pass in the above graph object as parameter so as to post data in feeds/ timeline/ activity?

If this is not the way, how should I do it?

Edit 1:

when I tried

graph = GraphAPI(request.facebook.user.oauth_token)
graph.post("me/namespace:action")

It showed me `OAuthError` 
Error Loc: C:\Python27\lib\site-packages\facepy\graph_api.py in _parse, line 274


graph = GraphAPI(request.facebook.user.oauth_token)
graph.post("me/namespace:action", "object type")

It showed me `TypeError`
loc: same as previous

Edit 2:

Instead to using request.facebook.user.oauth_token, I directly used my access token and the code worked..

graph = GraphAPI (my-hard-coded-access-token)
graph.post("me/feed", message = "working!")

However, when I tried

graph.post("me/news.reads", article="working")

It showed me error. 

Solution

  • I created open graph actions

    You shouldn't be using me/feed then

    The OpenGraph calls are as follows

    me/[app_namespace]:[action_type]?[object_type]=[OBJECT_URL]
    

    So, to achieve the same just set the path to me/[app_namespace]:[action_type]

    graph.post(
            path = 'me/[app_namespace]:[action_type]',
            [object_type] = '[OBJECT_URL]'
    )
    

    og_data is not a parameter in the call. So if your object_type is recipe then it would be

    graph.post(path, recipe)
    

    If you want to continue to use me/feed then it should be

    graph.post(path, link)
    

    as described at http://developers.facebook.com/docs/reference/api/user/#posts


    You cannot do customs read actions like that, the proper call should be

    graph.post(
        path = 'me/news.reads',
        article = 'http://yourobjecturl/article.html'
    )
    

    Please read the documentation http://developers.facebook.com/docs/opengraph/actions/builtin/#read