2014-11-04 165 views
1

与Facebook认证具有Oauthlib身份验证时,在Facebook上建立关示例代码here的时候,我一直得到错误的“从Facebook的响应无效”时,“从Facebook无效的响应”。使用烧瓶Oauthlib

我列出以下相关代码的章节。

设置:

设置所述的Oauth请求对象。

不图:导航路线和瓶的应用程序初始化。

from flask_oauthlib.client import OAuth, OAuthException 

oauth = OAuth() 

facebook = oauth.remote_app('facebook', 
    base_url='https://graph.facebook.com/', 
    request_token_url=None, 
    access_token_url='/oauth/access_token', 
    authorize_url='https://www.facebook.com/dialog/oauth', 
    consumer_key=config.get("app_id"), 
    consumer_secret=config.get("app_secret"), 
    request_token_params={'scope': 'public_profile,email'} 
) 

@facebook.tokengetter 
def get_facebook_token(): 
    if current_user.is_authenticated(): 
    return current_user.get_facebook_token() 

    else: 
    return None 

登录处理程序:

发送用户位置,以便开始的过程中,随着网址附加到根URL facebook的回调。

@app.route('/facebook/login') 
def facebook_login(): 
    return facebook.authorize(callback="http://example.com%s" % url_for('facebook_callback')) 

Facebook的回调,这个问题的根源:

在这里,我可以争取一个代码(大概是令牌)返回,但Oauthlib无法正确解析它。

@app.route('/facebook/callback') 
def facebook_callback(response): 
    response = facebook.authorized_response() 
    if response is None: 
    flash("You denied the request to sign in.", "error") 
    return redirect(url_for('index')) 

    if isinstance(response, OAuthException):  
    flash("Access denied: %s" % response.message, "error") 
    return redirect(url_for('index')) 

    # Request fails here, returns the redirect above. 

被引导到Facebook的后倾的要求ARGS我可以看到很清楚,并成功连接,还有一个很长的令牌返回到沿线的回调“?码= 1234567890,abcdefghijklmnop” ,但实际上尝试与此验证失败与“来自Facebook的无效回应”。

下面是一个简单的请求转储:

ImmutableMultiDict([('code', 'AQAPedwWavTb_cBx6NVy-qy3AL5YPr780oG5tA1LfITpVwkk-kr_4I0lG6B-vlhcYEubhuKhEM46bPs-1WpWUpJzcWfhkQ64lIkdLm9uSHSqeBHBM_6zw7SDpvVmGK-JKWBpAqRJuBKenl9zslQizthox96104iiul0uYQY67cmZgPXZi9uL-mcgZ5dRj387eKJIjNninBXxwCGgFkg5kLVHYt7t0ktUH58stYlxn2f98AXuSlrIvWsA5NeHsVbM8XY0XQrDrNbCvjDmEwHQGkZ3uZRbyaecN7MAi0bM0TrZzpuQ8j3M34DnQp_v9n4ktM4')]) 

曾使用过基于Twitter的样品的作品,我想这可能是一个可能的漏洞库,由于Facebook的API的变化类似的代码,但我将不胜感激任何指针!

回答

0

对于任何人谁在这个由谷歌在未来绊倒,我可读取here的解决方案解决了这个。

嘿,我在一个非常哈克的方式,我不会 建议在生产环境中解决了这个问题,但我终于找到了 问题我最后的消息后几天。

当你问Facebook的访问令牌,它不会给你在你所期望的方式的 访问令牌。我认为是Facebook的 失败是一个(可能是故意的) 格式错误。

你可以期待什么:

http://example.com/callback?access_token=00000000000

http://example.com/callback与作为一个标题POST 参数传递的访问令牌。

什么实际上情况是,Facebook的这样的回应:

http://example.com/callback?#access_token=0000000000

正因为如此,它是 - 不可能 - 对于任何服务器端语言 分析它,因为访问令牌现在只能在 浏览器中看到。它不会传递到后端。

捕获请求:

@app.route('/facebook/translate', methods=['GET']) 
def facebook_translate(): 
    # Facebook responds with the access token as ?#access_token, 
    # rather than ?access_token, which is only accessible to the browser. 
    # This part is where things get really, really dumb. 
    return ''' <script type="text/javascript"> 
    var token = window.location.href.split("access_token=")[1]; 
    window.location = "/facebook/callback?access_token=" + token; 
    </script> ''' 

论文集照例:

@app.route('/facebook/callback', methods=['GET', 'POST']) 
def facebook_callback(): 
    access_token = request.args.get("access_token") 

    if access_token == "undefined": 
    flash("You denied the request to sign in.", "error") 
    return redirect(url_for('index')) 

    graph = facebooksdk.GraphAPI(access_token) 
    profile = graph.get_object("me")