簡體   English   中英

Flask Facebook 登錄使用 Oauthlib - 重定向問題

[英]Flask Facebook Login using Oauthlib - redirect problem

我想使用 Flask 實現 Facebook 登錄。 這是調用 Facebook 請求服務的函數

@users_view.route('/facebook/')
def facebook():
    credentials = current_app.config['OAUTH_CREDENTIALS']['facebook']
    f_client = WebApplicationClient(credentials['id'])
    authorization_endpoint = 'https://www.facebook.com/dialog/oauth'

    request_uri = f_client.prepare_request_uri(
        authorization_endpoint,
        redirect_uri=request.base_url + "/auth",
        scope=["email"],
        auth_type = "reauthenticate",
    )
    print("REQUEST: {}".format(request_uri))
    return redirect(request_uri)

@users_view.route("/facebook/auth")
def facebook_callback():
    credentials = current_app.config['OAUTH_CREDENTIALS']['facebook']
    f_client = WebApplicationClient(credentials['id'])
    token_endpoint = 'https://graph.facebook.com/oauth/access_token'
    code = request.args.get("code")

    token_url, headers, body = f_client.prepare_token_request(
    token_endpoint,
    authorization_response=request.url,
    redirect_url=request.base_url,
    code=code   
    )
    print("ALL: url:{} headers:{} url:{} ".format(token_url, headers, body))

它將我轉發到這個 URL:

https://www.facebook.com/dialog/oauth?response_type=code&client_id=5453357158093262&redirect_uri=https%3A%2F%2F127.0.0.1%3A5000%2Fuser%2Ffacebook%2F%2Fauth&scope=email&auth_type=reauthenticate&ret=login&fbapp_pres=0&logger_id=1cc03c7d-9a19-43ba-978c-4ed8cb7aa559&tp=unspecified&cbt=1663931173196&ext=1663941992&hash=AeaYsntT-4HEQj4ZtfI

拋出以下錯誤:

在此處輸入圖像描述

在我的 Facebook 開發者帳戶中,我有以下重定向 URL 配置:

在此處輸入圖像描述

請建議我如何解決這個問題。

Facebook API 期望請求者使用您在 Facebook 開發者帳戶中設置的 HTTPS。

可能您正在使用 HTTP 協議(而非 HTTPS)運行您的 Flask 應用程序,而在您的 Facebook 開發者帳戶上您只將 HTTPS 列入白名單,但由於您沒有指定 HTTP,它將被拒絕。

A) 嘗試允許來自 Facebook 面板的 HTTP(無 S):
http://localhost:5000/whatever_you_need/ 和http://127.0.0.1:5000/whatever_you_need/
即使 localhost 和 127.0.0.1 是同義詞,也要添加它們。

- - 要么 - -

B) 安裝 pyOpenSSL

pip3 install pyOpenSSL

創建 ssl_my_app.py 並使用 ssl_context 運行:

from flaskr import create_app
from flask import current_app, g
import sqlite3 # if using databases

app = create_app()

with app.app_context():
    g.db = sqlite3.connect(
        current_app.config['DATABASE'],
        detect_types=sqlite3.PARSE_DECLTYPES
    )

    g.db.row_factory = sqlite3.Row

    with current_app.open_resource('schema.sql') as f:
        g.db.executescript(f.read().decode('utf-8'))

app.run(ssl_context='adhoc')

使用 ssl_my_app.py 運行應用程序:

python3 ssl_my_app.py

這將使用 HTTPS(自簽名證書)運行應用程序。 因此,當您調用 Facebook API 時,您的應用程序的請求將在白名單中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM