如何使用django social_auth在google oauth登录后打开原始页面

时间:2015-07-07 10:50:56

标签: python django oauth

我使用django social_auth进行用户识别,并且在用户首次登录时效果很好。但是,如果访问令牌已过期或按网址直接打开,则用户将在google oauth之后重定向到默认页面(' /')。在google oauth登录后,我该怎么做才能获得原始页面?

这是我的settings.py:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',    
'bootstrap3',
'djangobower',
'social_auth',
)

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.google.GoogleOAuth2Backend',
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
GOOGLE_OAUTH2_CLIENT_ID = 'CLIENT_ID'
GOOGLE_OAUTH2_CLIENT_SECRET = 'CLIENT_SECRET'
SOCIAL_AUTH_USER_MODEL = 'auth.User'
SESSION_SERIALIZER='django.contrib.sessions.serializers.PickleSerializer'
GOOGLE_WHITE_LISTED_DOMAINS = ['google.com']

SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {
    'access_type': 'offline',
    'approval_prompt': 'select_account+consent',
    'prompt' : 'select_account+consent'
}

的login.html:

{% load bootstrap3 %}
{% load static %}
<link media="all" href="{% static 'bootstrap/dist/css/bootstrap.min.css' %}" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src='{% static 'bootstrap/dist/js/bootstrap.min.js' %}'></script>
{% load static %}    
<script type="text/javascript" src='{% static 'jquery/jquery.js' %}'></script>
<link media="all" href="{% static 'dashboard.css' %}" type="text/css" rel="stylesheet"/>
<script src='{% static 'canvasjs/canvasjs.min.js' %}' type="text/javascript"></script>
<link rel="stylesheet" href="{% static 'signin.css' %}" /> 

{% if form.errors %}
<p text-align="center">Your username and password didn't match. Please try again. {{form.errors}} </p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-signin" role="form">
{% csrf_token %}
<h2 class="form-signin-heading">ABC Website</h2>
<input id="id_username" maxlength="254" name="username" type="text" class="form-control" placeholder="Username" autofocus/>
<input id="id_password" name="password" type="password" class="form-control" placeholder="Password"/>
<button class="btn btn-lg btn-primary btn-block" type="submit">System Account LogIn</button>
<input type="hidden" name="next" value="{{ next }}" />
</form>

<p style="text-align: center"><a href="{% url "socialauth_begin" "google-oauth2" %}?prompt=select_account+consent">GMail Login</a></p>

urls.py:

url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
url(r'^accounts/logout/$', login_required(views.logout_view), name = "logout"),    
url(r'^complete/(?P<backend>[^/]+)/$', views.AuthComplete.as_view()),
url(r'^login-error/$', views.LoginError.as_view(), 'login-error'),
url(r'', include('social_auth.urls')),    
url(r'^admin/', include(admin.site.urls)),

2 个答案:

答案 0 :(得分:2)

在您的html模板中,您可以在登录按钮中将其重定向到同一页面:

href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path }}"

“next =”指定登录后链接到下一个的位置。所以“next = {{request.path}}”链接到与原始请求相同的路径。

如果您只是重定向到主页,那么只需使用“next = /”:

href="{% url 'social:begin' 'google-oauth2' %}?next=/"

答案 1 :(得分:0)

对我有用的是重写func filterImgTag(text: String) -> String{ return text.replacingOccurrences(of: "<img[^>]*>", with: "", options: String.CompareOptions.regularExpression) } 函数 在CBV中执行:

get_context_data

然后在模板中:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    next_page = self.request.GET.get('next')
    context['next'] = next_page
    return context