我在Python 2.7(使用Anaconda发行版)中有以下简单示例,用于使用Facebook Graph API访问数据:
import facebook
token = 'mytoken'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
最初,我得到的"模块"对象没有属性"GraphAPI",并注意到Facebook的预填充方法列表基本上是空的。 这导致我采纳了这篇文章的建议,卸载了facebook并安装了facebook-sdk。 从某种意义上说,这在现在 GraphAPI 显示在预填充列表中,但我仍然收到以下错误:
AttributeError: 'module' object has no attribute 'GraphAPI'
对于我可能忽略的这个问题,还有其他解决方案吗?
这是一个已知问题。只需卸载 facebook 和 facebook-sdk,然后重新安装 facebook-sdk。
GraphApi模块是否存在于pyfacebook
中Python Facebook SDK:"module"对象没有属性"GraphAPI"
更新1:
我想我现在明白了。我认为您没有使用令牌。试试这个:
import facebook
token = 'mytoken'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
可能很明显,但请确保您的 python 脚本没有被命名为 facebook.py,因为它会与包冲突并导致此错误。