django - 当我在 Django 项目中使用时,为什么 HttpResponseRedirect.set_cookie 不起作用?

标签 django cookies

当我使用Google OAuth验证我的用户时,验证通过后,我想重定向到用户在授权之前访问的页面,所以我想将页面路径保存到用户的cookie中,所以我的实现是这样的:

def get_login_resp(request, redirect):
    print(redirect)
    auth_url = "https://accounts.google.com/o/oauth2/auth?" + urlencode({
        "client_id": GOOGLE_CLIENT_ID,
        "response_type": "code",
        "redirect_uri": make_redirect_url(request, redirect),
        "scope": "profile email",
        "max_auth_age": 0
    })
    resp = HttpResponseRedirect(auth_url)
    max_age = 3600 * 24
    expires = datetime.strftime(datetime.utcnow() + timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
    print(expires)
    resp.set_cookie('google_auth_redirect', redirect, max_age=max_age, expires=expires,
                    domain=LOGIN_COOKIE_DOMAIN, secure=True, httponly=True)
    print(resp._headers)
    print(resp.cookies)
    return resp

ps:redirect 是我要保存的页面路径

但是当使用 Postman 请求登录 url 时,我只能看到这个标题:
response headers

还有这些 cookies :
Cookies

那么我该如何解决这个问题呢?我没有任何错误信息。

最佳答案

尝试各种方法以找出问题所在,但仍然失败。

所以我尝试在另一台机器(Linux 服务器)上运行服务器,它工作正常!!!

顺便说一句:我的开发 PC 是 Macbook Pro 15 英寸,2017 年,装有 macOS High Sierra 10.13.1

2020 年 1 月 14 日更新:

没有找到根本原因,但我通过将redirect_url 保存到 session 数据中解决了这个问题 ,在此解决方案中,您应该使用另一个请求检查验证是否有效,然后再次调用 google auth 进行重新验证,代码如下:

class GoogleAuthView(RedirectView):
# google auth view

    def get(self, request, *args, **kwargs):
        # get redirect url from url params, frontend code should pass the param in request url
        redirect_url = request.GET.get('redirect_url', None)
        if redirect_url:
            redirect_url = parse.unquote(redirect_url)
        credentials = request.session.get("credentials", None)
        if (not credentials) or ('expire_time' not in credentials) or (credentials['expire_time'] < time.time()):
            request.session['redirect_url'] = redirect_url  # if need google auth, save redirect url to session first
        else:
            if redirect_url:
                return HttpResponseRedirect(redirect_url)

        flow = google_auth_oauthlib.flow.Flow.from_client_config(
            client_config=settings.GOOGLE_AUTH_CONFIG,
            scopes=settings.GOOGLE_AUTH_SCOPES
        )
        flow.redirect_uri = settings.GOOGLE_AUTH_CONFIG['web']['redirect_uris'][0]
        authorization_url, state = flow.authorization_url(
            access_type='offline',
            include_granted_scopes='true'
        )
        request.session['state'] = state
        return HttpResponseRedirect(authorization_url)


class GoogleAuthCallBackView(BasicView):
# google callback view

    def get(self, request, *args, **kwargs):
        state = request.session.get('state')
        flow = google_auth_oauthlib.flow.Flow.from_client_config(
            client_config=settings.GOOGLE_AUTH_CONFIG,
            scopes=settings.GOOGLE_AUTH_SCOPES,
            state=state
        )
        flow.redirect_uri = settings.GOOGLE_AUTH_CONFIG['web']['redirect_uris'][0]

        # get redirect url from session data if exists
        redirect_url = request.session.get('redirect_url') or settings.ADMIN_LOGIN_REDIRECT_URL
        response = HttpResponseRedirect(redirect_url)
        try:
            del request.session['redirect_url']
        except KeyError:
            logger.info('Delete `redirect_url` in session get KeyError.')
            pass

        try:

            flow.fetch_token(authorization_response=request.build_absolute_uri())
        except Exception as e:
            logger.error(e.message)
            return response

        # save credentials to session
        credentials = flow.credentials
        request.session["credentials"] = {
            'token': credentials.token,
            'refresh_token': credentials.refresh_token,
            'token_uri': credentials.token_uri,
            'client_id': credentials.client_id,
            'client_secret': credentials.client_secret,
            'scopes': credentials.scopes,
            'expire_time': time.time() + TOKEN_EXPIRE_TIME,
        }

        profile_client = googleapiclient.discovery.build(
            serviceName='oauth2',
            version='v2',
            credentials=credentials
        )

        profile = profile_client.userinfo().v2().me().get().execute()
        email = profile['email']
        user = user_manager.get_user_by_email(email)

        if user:
            user.username = profile['name']  # sync username from google
            user.picture = profile['picture']  # sync avatar from google
            user.save()
            request.session["user"] = user.to_dict()
        else:
            return HttpResponseRedirect("/api/non_existent_user/")  # show non-existent user

        return response

关于django - 当我在 Django 项目中使用时,为什么 HttpResponseRedirect.set_cookie 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447159/

相关文章:

javascript - 自动选择加入 cookie 的浏览器插件 - 欧盟 cookie 法

c# - 如何使用 ASP.NET Core 2.0 存储和恢复 cookie?

django - 南方是包的时候不认型号

python - Django 预取与最大值过滤器相关

django - 用于 Django 容器 SMTP 后端的 Docker SMTP 服务器

python - 是否有 CherryPy 等同于 Django 的消息框架(MessageMiddleware)?

javascript - 如何使用 Fetch API 获取/设置多个 'Set-Cookie' header ?

Django rest框架,在同一个ModelViewSet中使用不同的序列化器

cookies - 如何保存 cookie 并将其加载到另一个 puppeteer session 中?

asp.net-mvc - ASP.NET核心: session expires but user is not redirected to login page