一旦OAuth舞蹈用FlaskDanceSQLA完成,最好的方法是实现实际的API?

2024-04-26 04:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在工作:https://github.com/singingwolfboy/flask-dance-google-sqla并让OAuth模板舞开始工作。你知道吗

我一直在尝试使用一些代码来实现googlecalendarapi,用模板的设置方式来实现这一点,最好的方法是什么?你知道吗

这是正确的方向吗?或者有一个会话对象可以直接使用附加的头吗?你知道吗

我一直在尝试获取令牌(从SQLAlchemy数据库)和执行请求.post是的,但是找不到一个。你知道吗

def get_token():
    return OAuth.query.filter_by(user_id=current_user.id).first().token

@app.route("/get_events")
def get_events():
    token = str(get_token())
    req = requests.post('https://www.googleapis.com/calendar/v3', headers={'Authorization': token})

Tags: httpsgithubcomtoken模板idflaskget
1条回答
网友
1楼 · 发布于 2024-04-26 04:29:27

使用Flask Dance提供的google会话对象要容易得多,如下所示:

from flask_dance.contrib.google import google

@app.route("/get_events")
def get_events():
    if not google.authorized:
        return redirect(url_for("google.login"))
    req = google.post("/calendar/v3")
    # ... do whatever you want with this data

googlesession对象将自动从存储中加载OAuth令牌(在您的例子中是SQLAlchemy)。但是,它只能在过去某个时候存储了令牌的情况下加载它,这就是为什么最好检查google.authorized如果它为False,那么就没有令牌。你知道吗

这有道理吗?你知道吗

相关问题 更多 >