获取“AuthStateMissing ...会话值状态丢失。” 当将 AtlassianOAuth2 与 Django 一起使用时要调用回调 URI

Fra*_*son 5 django oauth django-authentication jira-rest-api python-social-auth

我试图在 Django 应用程序中设置 oauth2 身份验证。这是我的设置:

\n\n
*other parts ommited*\n# AUTH STUFF\n\nAUTHENTICATION_BACKENDS = (\n    \'social_core.backends.atlassian.AtlassianOAuth2\',\n    \'django.contrib.auth.backends.ModelBackend\',\n)\n\nSOCIAL_AUTH_ATLASSIAN_KEY = \' *my atlassian key here* \'\nSOCIAL_AUTH_ATLASSIAN_KEY_SECRET = \' *my atlassian secret key here* \'\nLOGIN_URL = \'/auth/login/atlassian-oauth2\'\nLOGIN_REDIRECT_URL = \'/\'\nLOGOUT_REDIRECT_URL = \'/\'\nSOCIAL_AUTH_URL_NAMESPACE = \'social\'\n\nSESSION_COOKIE_SECURE = False\n# i had to do that^, based on what i have read from\n# /sf/ask/2633211591/\n# but it still doesn\'t work, sadly...\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的登录页面视图:

\n\n
def index(request):\n    session_id = request.session.session_key\n    session_id = hashlib.sha256(str(session_id).encode(\'utf-8\')).hexdigest()\n    auth_url = \'https://auth.atlassian.com/authorize?audience=api.atlassian.com&client_id=*my_client_id_here*&scope=read%3Ajira-user%20read%3Ajira-work%20manage%3Ajira-project&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fcomplete%2Fatlassian%2F&state=$\'+ session_id +\'&response_type=code&prompt=consent\'\n    print(auth_url)\n    context = {\n        \'message\': \'You are now authenticated\'\n        if request.user.is_authenticated else \'You are not authenticated\',\n        \'auth_url\': auth_url\n    }\n    return render(request, \'core/home.html\', context)\n
Run Code Online (Sandbox Code Playgroud)\n\n

解释下面的内容 -

\n我之前用于授权授予的 url 只是:
\n<a href="{% url "social:begin" "* name of backend here *" %}">
来自文档https://python-social-auth-docs.readthedocs.io/en/latest/configuration/django .html
它对我来说适用于 facebook 和 google - 但不适用于 atlassian。所以我检查了 atlassian oauth2 指南(https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/),它说我必须使用Jira Platform Rest API 授权 URL。所以它对我有用。我能够访问要求 Atlassian 用户授权的页面。

\n当我单击接受时,Django 给我一个错误,显示“AuthStateMissing at /complete/atlassian/”。回溯表明它raise AuthStateMissing(self, \'state\')。我从 Atlassian 指南中读到,我必须有一个状态,即“与您定向到授权 URL 的用户关联的值,例如 user\xe2\x80\x99s 会话 ID 的哈希值”,因此我获取了用户 cookie 的哈希值,然后将其放入 auth_url ——但它仍然不起作用。

这是请求信息:回调URI后面的请求信息\n

这是来自终端的消息:

\n\n
[15/May/2019 02:36:13] "GET /home/ HTTP/1.1" 200 1008\nInternal Server Error: /complete/atlassian/\nTraceback (most recent call last):\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner\n    response = get_response(request)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response\n    response = self.process_exception_by_middleware(e, request)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func\n    response = view_func(request, *args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view\n    return view_func(*args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_django/utils.py", line 49, in wrapper\n    return func(request, backend, *args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_django/views.py", line 33, in complete\n    *args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_core/actions.py", line 43, in do_complete\n    user = backend.complete(user=user, *args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_core/backends/base.py", line 40, in complete\n    return self.auth_complete(*args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_core/utils.py", line 259, in wrapper\n    return func(*args, **kwargs)\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_core/backends/oauth.py", line 388, in auth_complete\n    state = self.validate_state()\n  File "/home/vasiliy/.virtualenvs/dj_atlassian/lib/python3.7/site-packages/social_core/backends/oauth.py", line 90, in validate_state\n    raise AuthStateMissing(self, \'state\')\nsocial_core.exceptions.AuthStateMissing: Session value state missing.\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

真的希望你们能帮忙。谢谢

\n

kn3*_*n3l 2

在我的 settings.py 文件中。

SOCIAL_AUTH_REDIRECT_IS_HTTPS = True

我的生产服务器使用 nginx 将 HTTP 重定向到 HTTPS,这就是会话状态丢失的原因。祝你好运,希望这会有帮助!