Python,Facebook Graph API - AttributeError:' GraphAPI'对象没有属性' search'

时间:2017-09-17 05:41:57

标签: python facebook

这个问题已被提出,但从未回答过。

import facebook
graph = facebook.GraphAPI(access_token="your token",version="2.7")

在Facebook SDK python页面中,我得到了以下代码:

# Search for places near 1 Hacker Way in Menlo Park, California.
places = graph.search(type='place',
                      center='37.4845306,-122.1498183',
                      fields='name,location')

# Each given id maps to an object the contains the requested fields.
for place in places['data']:
    print('%s %s' % (place['name'].encode(),place['location'].get('zip')))

这里是link

然而,它并没有起作用。我不明白为什么。错误读取

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-56959958831f> in <module>()
      1 # Search for places near 1 Hacker Way in Menlo Park, California.
----> 2 places = graph.search(type='place',
      3                       center='37.4845306,-122.1498183',
      4                       fields='name,location')
      5 

AttributeError: 'GraphAPI' object has no attribute 'search'

这是什么意思?为什么这个例子不起作用?我似乎无法找到有关GraphAPI类结构的细节的文档,但我认为搜索是其中的一部分。

2 个答案:

答案 0 :(得分:6)

这是因为自2016年以来,软件包所有者尚未更新此SDK的正式版本。

https://pypi.python.org/pypi/facebook-sdk

所以最新版本是2.0.0。

pip freeze | grep "facebook-sdk"
facebook-sdk==2.0.0

如果您想继续使用此套餐,则需要关注installation instructions for the git repo

virtualenv facebookenv
source facebookenv/bin/activate
pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk

然后在Python中,您应该能够正常使用它

>>> import facebook
>>> graph = facebook.GraphAPI(access_token="YOUR_TOKEN", version="2.10")
>>> graph.search(type='place', center='37.4845306,-122.1498183', fields='name,location')
{u'paging': {u'cursors': {u'after': u'MjQZD'}, u'next': u'https://graph.facebook.com/v2.10/search?access_token=YOUR_TOKEN&fields=name%2Clocation&type=place&center=37.4845306%2C-122.1498183&limit=25&after=MjQZD'}, u'data': [{u'id': u'166793820034304', u'name': u'Facebook HQ', u'location': {u'city': u'Menlo Park', u'zip': u'94025', u'country': u'United States', u'longitude': -122.1501, u'state': u'CA', u'street': ...

答案 1 :(得分:0)

我在wall-post命令中遇到了同样的问题,facebook-sdk 3+则无法正常工作

pip install facebook-sdk == 2.0.0修复了该问题

pip uninstall facebook-sdk
pip install facebook-sdk==2.0.0