如何获取页面访问令牌

时间:2012-11-14 18:54:57

标签: python django facebook facebook-graph-api

我在Facebook和我的个人资料页面中创建应用。在“选择你的应用程序如何与Facebook集成”部分我没有选择任何选项,因为我只想将文本发布到Facebook页面(也许这是问题?)。 我有这段代码:

FACEBOOK_APP_ID = 'myappid'
FACEBOOK_APP_SECRET = 'myappsecret'
FACEBOOK_PROFILE_ID = 'myprofileid'

oauth_args = dict(client_id     = FACEBOOK_APP_ID,
                  client_secret = FACEBOOK_APP_SECRET,
                  scope         = 'publish_stream',
                  grant_type    = 'client_credentials'
                  )

oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()

oauth_response看起来不错

但是当我跑步时:

resp = urllib.urlopen('https://graph.facebook.com/me/accounts?'+oauth_response).read()

我收到错误:

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

我做错了什么?我想在页面墙上张贴一些文字,例如,我点击我网站上的按钮(Django)。

更新:

好的,我在json中获取了页面数据。我解析它并获得page_access_token,但是当我这样称呼时:

attach = {
  "name": 'Hello world',
  "link": 'http://linktosite',
  "caption": 'test post',
  "description": 'some test'
}

facebook_graph = facebook.GraphAPI(page_access_token)
try:
    response = facebook_graph.put_wall_post('', attachment=attach)
except facebook.GraphAPIError as e:
    print e

我收到错误:“目标用户未授权此操作”

2 个答案:

答案 0 :(得分:2)

这个问题基本上是在询问同样的问题,答案似乎就是你要找的:(OAuthException - #2500) An active access token must be used to query information about the current user

答案 1 :(得分:0)

如果page_access_token是正确的,我猜你(页面管理员)还没有授权你的facebook应用程序将消息发布到facebook页面。

检查客户端的facebook登录功能,无论你是否要求足够的权限,范围选项应该是'mange_pages publish_stream photo_upload ...'取决于你的要求,而不仅仅是'mange_pages'