这个问题已被提出,但从未得到回答.
import facebook
graph = facebook.GraphAPI(access_token="your token",version="2.7")
Run Code Online (Sandbox Code Playgroud)
从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')))
Run Code Online (Sandbox Code Playgroud)
这是链接.
但它不起作用.我不明白为什么.错误读取
---------------------------------------------------------------------------
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'
Run Code Online (Sandbox Code Playgroud)
这是什么意思?为什么这个例子不起作用?我似乎无法找到有关GraphAPI类的结构细节的文档,但我认为搜索是其中的一部分.
这是因为自2016年以来,软件包所有者尚未更新此SDK的正式版本.
https://pypi.python.org/pypi/facebook-sdk
所以最新版本为2.0.0.
pip freeze | grep "facebook-sdk"
facebook-sdk==2.0.0
Run Code Online (Sandbox Code Playgroud)
如果您想继续使用此软件包,则需要遵循git repo的安装说明.
virtualenv facebookenv
source facebookenv/bin/activate
pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk
Run Code Online (Sandbox Code Playgroud)
然后在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¢er=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': ...
Run Code Online (Sandbox Code Playgroud)