为什么我无法从Facebook Graph API中看到对象的所有统计信息

时间:2016-06-23 20:24:21

标签: python facebook facebook-graph-api

我使用Python SDK for Facebook's Graph API来获取Facebook页面被喜欢的次数。我转到API Explorer获取访问令牌。我第一次选择" Graph API Explorer"从应用程序的下拉菜单(右上角)。然后我运行了这段代码并找回了我的预期:

import facebook

ACCESS_TOKEN = "**********"

facebook_page_id = "168926019255" # https://www.facebook.com/seriouseats/
graph = facebook.GraphAPI(ACCESS_TOKEN)
page = graph.get_object(facebook_page_id)

print page

{u'about': u'The Destination for Delicious',
 u'can_post': True,
 u'category': u'Website',
 u'checkins': 0,
 u'cover': {u'cover_id': u'10154881161274256',
  u'id': u'10154881161274256',
  u'offset_x': 0,
  u'offset_y': 43,
  u'source': u'https://scontent.xx.fbcdn.net/t31.0-0/p180x540/13391436_10154881161274256_2605145572103420621_o.jpg'},
 u'founded': u'December 2006',
 u'has_added_app': False,
 u'id': u'168926019255',
 u'is_community_page': False,
 u'is_published': True,
 u'likes': 159050,
 u'link': u'https://www.facebook.com/seriouseats/',
 u'mission': u'Serious Eats is a site focused on celebrating and sharing food enthusiasm through recipes, dining guides, and more! Our team of expert editors and contributors are the last word on all that\u2019s delicious.',
 u'name': u'Serious Eats',
 u'parking': {u'lot': 0, u'street': 0, u'valet': 0},
 u'talking_about_count': 3309,
 u'username': u'seriouseats',
 u'website': u'http://www.seriouseats.com',
 u'were_here_count': 0}

然后我回到API资源管理器并将应用程序更改为我最近创建的新Facebook应用程序。我生成了一个新的访问令牌,将其交换出来并运行上面的代码。这是我在page变量中得到的回复:

{u'id': u'168926019255', u'name': u'Serious Eats'}

如您所见,它只返回页面的idname,但缺少其他属性 - 特别是likes属性。

所以,我是否需要授予我的应用程序权限才能查看对象的所有属性?我已经尝试从我的应用ID&中生成访问令牌App Secret但仍然得到相同的结果。

1 个答案:

答案 0 :(得分:2)

这里有两件事要看。

  1. facebook API的版本。 在你得到大量结果的第一个例子中,你使用的是 2.2版(这是facebook python sdk的默认版本)。 当您在Facebook中创建新应用程序时,它很可能使用版本2.6 作为默认值。因此,它现在只返回两到三个字段,其余的你需要请求。

  2. 假设您确实使用的是2.6版,那么您可以要求的是

  3. 使用以下代码

    page = graph.get_object(id='168926019255', fields='about, affiliation, awards, category')
    

    这会给你

    {'id': '168926019255', 'about': 'The Destination for Delicious', 'category': 'Website'}
    

    现在你想得到喜欢的。由于喜欢不是默认字段,而是" edge",因此您需要使用" connection"来询问他们。为此,您可以执行以下操作:

    page = graph.get_connections(id='168926019255', connection_name='likes')
    

    这将为您提供所有喜欢

    {'data': [{'id': '134049266672525', 'name': 'Tom Colicchio'}, {'id': '143533645671591', 'name': 'Hearth'}, {'id': '57909700259', 'name': 'Toro'}, ....