1

我尝试将 Flask-Dance 与普通的烧瓶应用程序一起使用,它可以工作,如果我尝试用烧瓶蓝图实现它就不起作用。如何将烧瓶舞蹈注册到烧瓶蓝图?

我的views.py 用于授权蓝图

from flask import render_template, url_for, redirect, current_app, request
from app.auth import auth
from flask_dance.contrib import github

@auth.route('/login')
def login():
return render_template('auth/login.html')

@auth.route("/")
def github():
    if not github.authorized:
        return redirect(url_for("github.login"))
    resp = github.get("/user")
    assert resp.ok
    return "You are @{login} on GitHub".format(login=resp.json()["login"])

我用于身份验证蓝图的init .py

from flask import Blueprint
from flask_dance.contrib.github import make_github_blueprint, github

auth = Blueprint('auth', __name__, url_prefix='/auth')

blueprint = make_github_blueprint(client_id="m-client-id",client_secret="my-client-secret")

auth.register_blueprint(blueprint, url_prefix="/auth")



from app.auth import views

和我的主要init .py 文件:

from flask import Flask
from flask_fontawesome import FontAwesome

from app.config import Config

fa = FontAwesome()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)
    fa.init_app(app)
    
    from app.public import public
    app.register_blueprint(public)
    
    from app.auth import auth
    app.register_blueprint(auth)
    
    return app
4

1 回答 1

3

首先,您应该为 github 创建和注册不同的蓝图。

github/初始化.py

from flask_dance.contrib import github
from flask_dance.contrib.github import make_github_blueprint

github_blueprint = make_github_blueprint(client_id='your-client-id',client_secret='your-client-secret')



from app.github import views

github/views.py

@github_blueprint.route("/")
def github_login():
     if not github.authorized:
          return redirect(url_for('github.login'))
     account_info = github.get('/user')
     if account_info.ok:
          account = account_info.json()
          return '<h1>Your Github name is {}'.format(account['login'])

最后在你的主init .py 文件中

from flask import Flask
from flask_fontawesome import FontAwesome

from app.config import Config

fa = FontAwesome()

def create_app(config_class=Config):
      app = Flask(__name__)
      app.config.from_object(Config)
      fa.init_app(app)

      from app.public import public
      app.register_blueprint(public)

      from app.auth import auth
      app.register_blueprint(auth)

      from app.github import github_blueprint
      app.register_blueprint(github_blueprint,  url_prefix='/github_login')
      #/github_login=callback url

      return app
于 2021-05-23T02:02:21.907 回答