如何在python中解决facebook获取访问令牌错误?

时间:2015-11-04 13:58:14

标签: python facebook facebook-graph-api facebook-sdk-4.0

我已经通过控制台

在python中安装了facebook模块
pip install facebook-sdk

然后注册到facebook api以获取app_id和app_secret.I我在下面没有透露它们。

import facebook
app_id = '00000'
app_secret = 'xxxx'
access_token = facebook.get_app_access_token(app_id,app_secret)

当我执行它来获取access_token时,我的python控制台会抛出以下错误:

Traceback (most recent call last):

  File "<ipython-input-18-0dfc0fd92367>", line 1, in <module>
access_token = facebook.get_app_access_token(app_id,app_secret)

AttributeError: 'module' object has no attribute 'get_app_access_token'

请帮忙。

1 个答案:

答案 0 :(得分:5)

这是一个已知问题。只需卸载facebookfacebook-sdk,然后重新安装facebook-sdk

sudo pip uninstall facebook
sudo pip uninstall facebook-sdk
sudo pip install facebook-sdk

此外,您可以通过以下方式实施自己的get_app_access_token

def get_app_access_token(app_id, app_secret):
    """Get the access_token for the app.

    This token can be used for insights and creating test users.

    @arg app_id :type string :desc retrieved from the developer page
    @arg app_secret :type string :desc retrieved from the developer page

    Returns the application access_token.

    """
    # Get an app access token
    args = {'grant_type': 'client_credentials',
            'client_id': app_id,
            'client_secret': app_secret}

    f = urllib2.urlopen("https://graph.facebook.com/oauth/access_token?" +
                              urllib.urlencode(args))

    try:
        result = f.read().split("=")[1]
    finally:
        f.close()

    return result

结果将包含访问令牌