我应该如何让Open Graph JSON对象在facepy类中传递

时间:2012-07-14 16:33:17

标签: django facebook facebook-graph-api facebook-opengraph fandjango

我正在尝试在我的应用中配置Open Graph,以便当有人点击其中的“链接”时,应用应该在其Feed /时间线/活动上“发布”它。 ( I created open graph actions for these features and successfully added meta tags in the page 的)。

所以,我正在使用fandjangofacepy来完成这项工作。

这就是我的代码看起来......

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)

如何让开放式图形JSON obj作为参数传入上述graph对象,以便在Feed / timeline / activity中发布数据?

如果不是这样,我应该怎么做?

修改1:

当我尝试

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

编辑2:

而不是使用request.facebook.user.oauth_token,我直接使用了我的access token并且代码有效..

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

然而,当我尝试

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

It showed me error. 

2 个答案:

答案 0 :(得分:1)

  

我创建了开放图表操作

您不应该使用me/feed然后

OpenGraph调用如下

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

所以,要实现同样的目的,只需设置me/[app_namespace]:[action_type]

的路径即可
graph.post(
        path = 'me/[app_namespace]:[action_type]',
        [object_type] = '[OBJECT_URL]'
)

og_data不是通话中的参数。因此,如果您的object_typerecipe,那么它将是

graph.post(path, recipe)

如果您想继续使用me/feed,那么它应该是

graph.post(path, link)

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

所述

你不能做那样的海关阅读行动,正确的电话应该是

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

请阅读文档http://developers.facebook.com/docs/opengraph/actions/builtin/#read

答案 1 :(得分:0)

您正在获得OAuthError,因为您正在使用类的实例而不是字符串初始化Graph API客户端。

试试这个:

graph = GraphAPI(request.facebook.user.oauth_token.token)

或者,您可以使用Fandjango的graph模型的User属性来获取Graph API客户端的预初始化实例:

request.facebook.user.graph