如何在另一个文件中实例化我的变量

发布于 2025-01-09 17:49:32 字数 1104 浏览 0 评论 0原文

大家好,我正在尝试做一些非常简单的事情,但所有文档都在同一个文件上执行此操作。

我想在我的flask应用程序中使用oauth,这是我的main.py文件

from flask import Flask
from login_routes import login
from authlib.integrations.flask_client import OAuth

app = Flask(__name__)
oauth = OAuth(app)

app.register_blueprint(login)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

,我的login_routes.py有这些行

from flask import Blueprint, render_template, url_for, redirect, session
from authlib.integrations.flask_client import OAuth
import settings


# OAuth config
google =  oauth.register(
    name='google',
    client_id=settings.GOOGLE_CLIENT_ID,
    client_secret=settings.GOOGLE_CLIENT_SECRET,
    access_token_url=settings.GOOGLE_TOKEN_URI,
    access_token_params=None,
    authorize_url=settings.GOOGLE_AUTH_URI,
    authorize_params=None,
    api_base_url='https://www.googleapis.com/oauth2/v1/',
    client_kwargs={'scope': 'openid profile email'}
)

但是oauth没有在我的login_routes.py中声明,我不知道如何将该变量从主实例带到另一个实例文件中,我尝试导入 main.py ,这在我的大脑中毫无意义,因为最终会出现循环引用错误,请让我知道如何正确实例化它。

Hello everyone I'm trying to do something very simple but all the documentation out do this on the same file.

I want to use oauth in my flask app and this is my main.py file

from flask import Flask
from login_routes import login
from authlib.integrations.flask_client import OAuth

app = Flask(__name__)
oauth = OAuth(app)

app.register_blueprint(login)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

And my login_routes.py has these lines

from flask import Blueprint, render_template, url_for, redirect, session
from authlib.integrations.flask_client import OAuth
import settings


# OAuth config
google =  oauth.register(
    name='google',
    client_id=settings.GOOGLE_CLIENT_ID,
    client_secret=settings.GOOGLE_CLIENT_SECRET,
    access_token_url=settings.GOOGLE_TOKEN_URI,
    access_token_params=None,
    authorize_url=settings.GOOGLE_AUTH_URI,
    authorize_params=None,
    api_base_url='https://www.googleapis.com/oauth2/v1/',
    client_kwargs={'scope': 'openid profile email'}
)

However oauth is not declared in my login_routes.py, I dont know how to bring that variable from main instance to this other file, I tried importing main.py which on my brain makes no sense because that ends up on a circular reference error please let me know how to instantiate this correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2025-01-16 17:49:32

尝试

在 main.py 中的

app = Flask(__name__)
oauth = OAuth(app)

# assign your oauth to a variable (property) of app instance
app.OAUTH = oauth

login_routes.py 中

# This pulls in the app object
from flask import Blueprint, current_app as app

# Since you're registering 'login' as a blueprint in main.py, define it here
login = Blueprint('login', __name__)

# you can do access the oauth instance as app.OAUTH e.g
abc = app.OAUTH....

Try

In main.py

app = Flask(__name__)
oauth = OAuth(app)

# assign your oauth to a variable (property) of app instance
app.OAUTH = oauth

In login_routes.py

# This pulls in the app object
from flask import Blueprint, current_app as app

# Since you're registering 'login' as a blueprint in main.py, define it here
login = Blueprint('login', __name__)

# you can do access the oauth instance as app.OAUTH e.g
abc = app.OAUTH....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文