Python 微软图 API - /回调错误

Python microsoft graph API - /callback error

在我的 Django Web 应用程序中,我需要启用 SSO - 单租户 - 组织内部。

基于参考教程:link 我能够在 views.py、urls.py.

中复制粘贴所需的代码片段

我还创建了一个oauth_settings.yml文件-

app_id: {app id}
app_secret: {app secret}
redirect: "http://localhost:8000/callback"
scopes:
    - user.read
authority: "https://login.microsoftonline.com/{tenant}"

然而每次提交 O365 凭据后,我都会遇到相同的 /callback 错误 :-

我已经确定问题出在 'auth_flow' 变量中,该变量包含整个流字典。之前反映出来的数据,但是在request.session.

中保存失败

请指导手头的问题到底是什么。谢谢。

根据我的测试,当我们浏览到 http://127.0.0.1:8000 instead of http://localhost:8000, we got the error. Because the browser does not store session and set cookie when using IP address. So please use http://localhost:8000. to access project when you develop the project on your local machine. For more details, please refer to the Github issue

使用IP地址

使用本地主机

函数 def store_user() 中缺少提取 'timeZone' 的正确密钥。

转到文件 auth_helper.py 并将函数 store_user() 替换为以下内容:

def store_user(request, user):
  try:
    request.session['user'] = {
      'is_authenticated': True,
      'name': user['displayName'],
      'email': user['mail'] if (user['mail'] != None) else user['userPrincipalName'],
      'timeZone': user['mailboxSettings']['automaticRepliesSetting']['scheduledStartDateTime']['timeZone'] if (user['mailboxSettings']['automaticRepliesSetting']['scheduledStartDateTime']['timeZone'] != None) else 'UTC'
    }
  except Exception as e:
    print(e)