2014-11-04 162 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")