Python O365 Flask身份验证流程

时间:2020-07-16 06:59:28

标签: python flask

O365库(https://pypi.org/project/O365/)的文档具有以下示例Flask Web应用程序的代码段,用于登录并授予您的Azure应用程序的权限:

@route('/stepone')
def auth_step_one()

    callback = 'my absolute url to auth_step_two_callback'
    account = Account(credentials)
    url, state = account.con.get_authorization_url(requested_scopes=my_scopes
                                                   redirect_uri=callback)
    
    # the state must be saved somewhere as it will be needed later
    my_db.store_state(state) # example...
    
    return redirect(url)

@route('/steptwo')
def auth_step_two_callback():
    account = Account(credentials)
    
    # retreive the state saved in auth_step_one
    my_saved_state = my_db.get_state()  # example...
    
    # rebuild the redirect_uri used in auth_step_one
    callback = 'my absolute url to auth_step_two_callback'
    
    result = account.con.request_token(request.url, 
                                       state=my_saved_state,
                                       redirect_uri=callback)
    # if result is True, then authentication was succesful 
    #  and the auth token is stored in the token backend
    if result:
        return render_template('auth_complete.html')
    # else ....

但是,我对Flask不太熟悉(我以前只使用过Django),所以我不确定在这里的某些含义。

例如,在将route对象实例化为app.route之后,我收集到Flask应该是app。我不知道request中的account.con.request_token对象应该从哪里来,我也不知道'my absolute url to auth_step_two_callback'实际指的是什么,等等。

我知道这是一个很大的问题,但是有人可以写这个示例的工作版本(尽可能简单),并向我解释如何将其集成到控制台应用程序中吗?基本上,我只希望在默认的Web浏览器中打开azure身份验证网页,并在成功对flask应用程序进行身份验证后,将令牌无缝地传递回Account类,而无需用户复制粘贴使用基于控制台的身份验证界面的方式获取URL。

我用于Python(https://developers.google.com/gmail/api/quickstart/python)的google cloud API完全可以做到这一点,对于用户登录来说非常方便。

非常感谢。

0 个答案:

没有答案