django gmail login

Solutions on MaxInterview for django gmail login by the best coders in the world

showing results for - "django gmail login"
Luca
17 Oct 2016
1$ pip install django-allauth
2
3#settings.py
4
5TEMPLATES = [
6    {
7        'BACKEND': 'django.template.backends.django.DjangoTemplates',
8        'DIRS': [BASE_DIR / 'templates'],
9        'APP_DIRS': True,
10        #...
11    }
12]
13
14INSTALLED_APPS = [
15    #...
16    'allauth',
17    'allauth.account',
18    'allauth.socialaccount',
19    'allauth.socialaccount.providers.google',
20]
21
22AUTHENTICATION_BACKENDS = [
23    'django.contrib.auth.backends.ModelBackend',
24    'allauth.account.auth_backends.AuthenticationBackend'
25]
26
27SOCIALACCOUNT_PROVIDERS = {
28    'google': {
29        'SCOPE': [
30            'profile',
31            'email',
32        ],
33        'AUTH_PARAMS': {
34            'access_type': 'online', #set offline if you want background auth
35        }
36    }
37}
38
39SITE_ID = 2
40
41LOGIN_REDIRECT_URL = '/'
42LOGOUT_REDIRECT_URL = '/'
43
44#templates
45
46{% load socialaccount %}
47<html>
48<body>
49<h1>My Google OAuth Project </h1>
50{% if user.is_authenticated %}
51  <p>Welcome, You are logged in as {{ user.username }}</p>
52{% else %}
53  <a href="{% provider_login_url 'google' %}">Login With Google</a>
54{% endif %}
55</body>
56</html>
57
58#urls.py
59
60from django.urls import path, include
61from django.views.generic import TemplateView
62from django.contrib.auth.views import LogoutView
63
64urlpatterns = [
65    #...
66    path('', TemplateView.as_view(template_name="index.html")),
67    path('accounts/', include('allauth.urls')),
68    path('logout', LogoutView.as_view()),
69]
70
71#Register with Google Developer API
72https://console.developers.google.com/apis
73  
74#Then, add:
75http://127.0.0.1:8000 under Authorized JavaScript origins.
76http://127.0.0.1:8000/accounts/google/login/callback/ under Authorized redirect URIs.
77
78$ python manage.py migrate
79$ python manage.py createsuperuser
80$ python manage.py runserver
81"""Open http://127.0.0.1:8000/admin and login to Django Admin. 
82Under Sites click Add and put 127.0.0.1:8000 as both the Domain name and Display name.
83
84Then, under Social Applications click Add and fill in the details as follows:
85
86Provider: Google
87Name: OAuth App
88Client id: <The client ID you created in step 4>
89Secret key: <The Secret key you created in step 4>
90Sites: 127.0.0.1:8000
91Since you are currently logged in as a superuser, 
92logout and login again using your Google account.
93
94If you get an error: SocialApp matching query does not exist at 
95http://127.0.0.1:8000/accounts/google/login/,
96it means that the ID of the site you created in Django admin is not 
97the same as the one in settings.py. Consider playing around with the SITE_ID value.
98
99After signing in with Google, you can check the user information obtained from Google at:
100http://127.0.0.1:8000/admin/socialaccount/socialaccount/
101"""
similar questions
queries leading to this page
django gmail login