代码之家  ›  专栏  ›  技术社区  ›  RanRag Shibin k.Reeny

Python:脸书API显示无效令牌

  •  1
  • RanRag Shibin k.Reeny  · 技术社区  · 13 年前

    我正在使用 facepy facebook graph API 访问我的邮箱/邮件,我采用了以下两种方法:

    第一种方法:

    我用了 access token 我来自 Graph Explorer facebook页面,并使用以下代码:

    from facepy import GraphAPI
    graph = GraphAPI(token)
    print graph.get('/me')
    #Rest of the code
    

    上面的代码运行良好,我可以使用 FQL Query .问题出现在我授权时 expired 过了一段时间。

    所以,在谷歌上搜索了一下之后,我转到了 方法二 以下为:

    现在,我所做的是创建一个facebook应用程序 read_mailbox 得到了许可 id and key 然后使用 get_application_access_token 方法来获取令牌。

    检索到我使用的令牌后:

    token = facepy.utils.get_application_access_token(app_id, key)
    graph.get('/me')
    ## OUT: OAuthError: [2500] An active access token must be used to query information about the current user.
    facepy.utils.get_extended_access_token(token, app_id, key)
    # OUT: OAuthError: [1] No user access token specified
    

    现在,您可以看到错误( commented # )在使用应用程序令牌时生成。

    我相信我得到的错误是因为facebook需要 user_token 我正在提供 app_token

    那么,是否可以使用app_token访问用户数据?如果不能,如何发布 extended token 其可以访问用户数据。

    更新:

    因此,我遵循了@Johannes的建议,尝试了一下,但遇到了错误:

    from facepy.utils import get_extended_access_token
    from facepy import GraphAPI
    token = "My user access token got from https://developers.facebook.com/tools/explorer"
    long_lived_access_token = get_extended_access_token(token)
    graph = GraphAPI(long_lived_access_token)
    graph.get('/me')
    

    现在,当我运行上面的代码时,我得到了

    TypeError: get_extended_access_token() takes exactly 3 arguments (1 given)
    

    所以,我把它改成 long_lived_access_token = get_extended_access_token(token, None, None) 并且得到

    facepy.exceptions.OAuthError
    

    所以,我又把它改成了 long_lived_access_token = get_extended_access_token(token, app_id, key) 我得到了与上面相同的异常/错误。

    那么,这是一个bug还是我做错了什么。

    附言:我从git安装了最新版本。

    2 回复  |  直到 13 年前
        1
  •  5
  •   Johannes Gorset    13 年前

    您的假设是正确的,即您不能使用应用程序访问令牌来读取用户的邮箱,但您收到的错误源于您尚未初始化的事实 graph 根本没有访问令牌。

    尽管如此,您询问如何扩展用户的访问令牌是正确的。正如您已经发现的,Facepy HEAD(即将推出0.9版本)具有一个功能 get_extended_access_token 它接受现有的短期用户访问令牌并对其进行扩展。扩展的用户访问令牌持续2个月,您可以在 removal of offline_access permission

    如果您想使用 获取扩展访问权限 现在,您必须从git安装facepy:

    $ pip install git+git://github.com/jgorset/facepy.git@b5153f460f2f52cef9a5e49a3b48b3fb8742356c
    

    一旦安装了正确版本的Facepy,就可以扩展现有的短暂用户访问令牌,并初始化 GraphAPI 有了它:

    from facepy.utils import get_extended_access_token
    from facepy import GraphAPI
    
    long_lived_access_token, expires_at = get_extended_access_token(short_lived_access_token, application_id, application_secret_key)
    
    graph = GraphAPI(long_lived_access_token)
    graph.get('/me')
    
        2
  •  5
  •   RanRag Shibin k.Reeny    13 年前

    API没有什么问题,只是没有以正确的方式解释结果。 如果您尝试打印的结果 long_lived_access_token = get_extended_access_token(token) 它不会直接给你 long_lived_access_token 相反,它将为您提供一个包含内容的元组:

    long_lived_access_token = ('your token', datetime_object).
    

    您可以通过查看的源代码来验证这一点 utils.py .如果你看 get_extended_access_token 它返回的方法 token, expires_at

    根据 facebook docs 获得 extended 访问令牌必须在以下端点发出请求

    https://graph.facebook.com/oauth/access_token?             
        client_id=APP_ID&
        client_secret=APP_SECRET&
        grant_type=fb_exchange_token&
        fb_exchange_token=EXISTING_ACCESS_TOKEN 
    

    反应是这样的 token=mytoken&expire=5184000 哪里 5184000 指60天。

    因此,您的最终代码将如下所示:

    from facepy.utils import get_extended_access_token
    from facepy import GraphAPI
    
    app_id = 'id'
    key = 'key'
    
    short_lived_access_token = 'short_token'
    long_token = get_extended_access_token(short_token, id, key)
    
    graph = GraphAPI(long_token[0])
    print graph.get('/me')
    
    推荐文章