Django Oauth2 Facebook autentication

Asked

Viewed 30 times

0

When trying to include facebook login authorization on my site, I followed the steps taken in Django and similar documentations.

And doing step by step, including the app in Settings

"allauth.socialaccount.providers.facebook",

Including the app in the admin system and properly configuring the facebook app on the site, the next step was to include the configuration in html correctly :

{% load socialaccount %}
{% providers_media_js %}
<button><a href="{% provider_login_url "facebook" method="oauth2" %}">Login com Facebook</a></button>
</div>

When entering the login url on the site, however, the error :

ModuleNotFoundError at /accounts/login/
No module named 'path'
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/login/
Django Version: 3.1.3
Exception Type: ModuleNotFoundError
Exception Value:    
No module named 'path'

This error apparently remains even taking out of Settings, where after removing it is only :

KeyError at /accounts/login/
'facebook'
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/login/
Django Version: 3.1.3
Exception Type: KeyError
Exception Value:    
'facebook'
  • you need an extra module!

  • Ok ... How and where to make this extra module /

1 answer

0

Authentication using social media requires an extra module in the project. The server did not find an intermediary to do this and due to missing return this warning.

I state the social-auth-app-Django, to make this authentication,not only for mentioned social network more for which other want.

its configuration is well explained on the official page page

after completing the installation: Pip install social-auth-app-Django

It will be necessary to register in apps

INSTALLED_APPS = (
    ...
    'social_django',
    ...
)

On the official page, "There is a context processor that will add back-ends and association data to the model context:": this means that through the social_django will be added in the authentication context templates as name photo

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',#<==1
                'social_django.context_processors.login_redirect',#<==2
            ],
        },
    },
]

specify authentication by social network and no longer by Django

AUTHENTICATION_BACKENDS = [
    'social_core.backends.facebook.FacebookOAuth2',
   ]

Settings of the app developer facebook, when using this feature will not be recreated a new authentication for the user after logging out and start login again

SOCIAL_AUTH_FACEBOOK_KEY = 'sua chave'
SOCIAL_AUTH_FACEBOOK_SECRET = 'codigo'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', 'user_link'] #parametros a extrair
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id, name, email, picture.type(large), link'
}
SOCIAL_AUTH_FACEBOOK_EXTRA_DATA = [
    ('name', 'name'),
    ('email', 'email'),
    ('picture', 'picture'),
    ('link', 'profile_url'),
]

view login explanation page:

from django.views.generic import TemplateView

class LoginView(TemplateView):
    template_name = 'login.html'

in the app url:

from django.urls import path, include
from django.contrib.auth import views as auth_views # autenticação de views

from .views import  LoginView# campos de login 

urlpatterns = [
    path('login/', LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),# logout
    path('social-auth/', include('social_django.urls', namespace='social')), # api extra
 
]

html authentication using social network and not the Django standard:

   <a href="{% url 'social:begin' 'facebook' %}">rede social</a>
        </button>
          iniciar
        <button>
  • I get it, I’m implementing. But one question, I had already done with the social.account.providers of Django allauth and includes github as one of the providers, and it was working normally. It is necessary to remove the current Settings could remain with the same authorizer and only include this new module ?

  • not to problems tbm,

  • I can only include this new authorization then right ?

  • If you used another means of auth and use this together will not fail.

  • Apparently the error continues &#xA;NoReverseMatch at /accounts/login/&#xA;'social' is not a registered namespace&#xA;Request Method: GET&#xA;Request URL: http://127.0.0.1:8000/accounts/login/&#xA;

  • this is the routing.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.