调试AllAuth:尽管成功连接,社交帐户仍未记录用户 [英] Debugging AllAuth: social account not logging user in despite connecting successfully
问题描述
在登录表单中单击facebook登录按钮会正确显示fb登录弹出窗口,但输入凭据后弹出窗口将关闭,没有任何反应。
再次单击该按钮而不重新加载页面确认fb帐户已连接为浏览器控制台打印:
当用户已连接时调用FB.login()。
但是用户中没有新条目数据库,用户没有被重定向,也没有登录。所以问题似乎在于AllAuth如何处理事情。虽然后端的任何地方都没有显示调试信息,但这有点难以理解。
以下是allauth设置:
LOGIN_REDIRECT_URL ='/'
LOGOUT_REDIRECT_URL ='/'
DEFAULT_FROM_EMAIL =[已删除stackoverflow]
ACCOUNT_EMAIL_REQUIRED = True
SOCIALACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION ='强制'
SOCIALACCOUNT_EMAIL_VERIFICATION =必填
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = \
{'facebook':
{'METHOD':'oauth2',
'SCOPE':['email','public_profile','user_friends'],
'AUTH_PARAMS':{'auth_type':'reauthenticate'},
'FIELDS':[
'id',
'email',
'name',
'first_name',
' last_name',
'已验证',
'区域设置',
'时区',
'链接',
'related_time'],
'EXCHANGE_TOKEN ':是的,
'VERIFIED_EMAIL':错误,
'版本':'v2.4'}}
这是登录页面上的js:
< script>
window.fbAsyncInit = function(){
FB.init({
appId:'[stack for stackoverflow]',
xfbml:true,
version:' v2.10'
});
FB.AppEvents.logPageView();
};
(函数(d,s,id){
var js,fjs = d.getElementsByTagName(s)[0];
if(d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src =//connect.facebook.net/en_US/sdk.js;
fjs.parentNode.insertBefore(js,fjs);
}(document,'script','facebook-jssdk'));
< / script>
Django-allauth Facebook登录
首先,安装django-allauth
$(venv)pip3 install django-allauth
现在将这些代码放在settings.py中已安装的应用中
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
另外,将代码放在您的设置下面.py
AUTHENTICATION_BACKENDS =(
'allauth.account.auth_backends.AuthenticationBackend',
'django。 contrib.auth.backends.ModelBackend',
)
同样将这些放入设置中。 py
SOCIALACCOUNT_PROVIDERS = \
{'facebook':
{'METHOD':'oauth2' ,
'SCOPE':['email','public_p rofile','user_friends'],
'AUTH_PARAMS':{'auth_type':'reauthenticate'},
'FIELDS':[
'id',
'mail' ,
'名称',
'first_name',
'last_name',
'已验证',
'locale',
'timezone',
'链接',
'性别',
'related_time'],
'EXCHANGE_TOKEN':是的,
'LOCALE_FUNC':lambda请求:'kr_KR',
'VERIFIED_EMAIL':错误,
'版本':'v2.4'}}
ID和密钥可以在
保存申请后,我们准备用facebook登录。
将这些模板标签放在html的顶部,让用户登录。{%load socialaccount %}
{%providers_media_js%}
你可以把它写在你想要的位置制作登录按钮
< a href ={%provider_login_urlfacebookmethod =js_sdk%}>登录按钮图像< / a>
这会将您的请求发送到处理登录程序的帐户/登录页面。
参考:
https://medium.com/@jinkwon711/django-allauth-facebook-login-b536444cbc6b
Clicking the facebook login button in the login form correctly brings up the fb login popup, but after entering credentials the pop-up closes and nothing happens.
Clicking the button again without reloading the pages confirms that the fb account is connected as browser console prints:
FB.login() called when user is already connected.
However no new entries appear in the user database, the user isn't redirected and is not signed in. So the problem appears to be with how AllAuth is handling things. There's no debug info appearing anywhere on the back-end though, making this somewhat difficult to figure out.
Here's the allauth settings:
LOGIN_REDIRECT_URL = '/' LOGOUT_REDIRECT_URL = '/' DEFAULT_FROM_EMAIL = "[removed for stackoverflow]" ACCOUNT_EMAIL_REQUIRED = True SOCIALACCOUNT_EMAIL_REQUIRED = True ACCOUNT_UNIQUE_EMAIL = True ACCOUNT_EMAIL_VERIFICATION = 'mandatory' SOCIALACCOUNT_EMAIL_VERIFICATION = "mandatory" SOCIALACCOUNT_QUERY_EMAIL = True SOCIALACCOUNT_PROVIDERS = \ {'facebook': {'METHOD': 'oauth2', 'SCOPE': ['email','public_profile', 'user_friends'], 'AUTH_PARAMS': {'auth_type': 'reauthenticate'}, 'FIELDS': [ 'id', 'email', 'name', 'first_name', 'last_name', 'verified', 'locale', 'timezone', 'link', 'updated_time'], 'EXCHANGE_TOKEN': True, 'VERIFIED_EMAIL': False, 'VERSION': 'v2.4'}}
and this is the js on the login page:
<script> window.fbAsyncInit = function() { FB.init({ appId : '[removed for stackoverflow]', xfbml : true, version : 'v2.10' }); FB.AppEvents.logPageView(); }; (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script>
解决方案Django-allauth Facebook Login
First, install django-allauth
$(venv) pip3 install django-allauth
now put these code on your installed apps in settings.py
'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.facebook',
Also, put code below to your settings.py
AUTHENTICATION_BACKENDS = ( 'allauth.account.auth_backends.AuthenticationBackend', 'django.contrib.auth.backends.ModelBackend', )
Also put these in the settings.py
SOCIALACCOUNT_PROVIDERS = \ {'facebook': {'METHOD': 'oauth2', 'SCOPE': ['email','public_profile', 'user_friends'], 'AUTH_PARAMS': {'auth_type': 'reauthenticate'}, 'FIELDS': [ 'id', 'email', 'name', 'first_name', 'last_name', 'verified', 'locale', 'timezone', 'link', 'gender', 'updated_time'], 'EXCHANGE_TOKEN': True, 'LOCALE_FUNC': lambda request: 'kr_KR', 'VERIFIED_EMAIL': False, 'VERSION': 'v2.4'}}
ID and Key can be found at https://developers.facebook.com/ create an app and go to DashBoard, you’ll see your’s
#facebook SOCIAL_AUTH_FACEBOOK_KEY = 'secret!' # App ID SOCIAL_AUTH_FACEBOOK_SECRET ='secret!' #app key
You might want to add this
LOGIN_REDIRECT_URL = "/" #if you succeed in login, you'll be redirected to the main page.
Now we need to modify the urls.py Add this code to your urlpatterens
url(r'^accounts/',include('allauth.urls')),
Now go back to https://developers.facebook.com/ go to settings ,click "Add platform", click website, put http://localhost:8000/ and click the quick start button. keep going and do what dev.facebook leads to.
Now you need to set your site id in settings.py
#site id SITE_ID = <your local host site id> #i.e http://localhost:8000 # for the dev mode, you need to use localhost's id facebook does not support the name 127.0.0.1:8000 #little options for your page's signup. ACCOUNT_EMAIL_REQUIRED=True
We need to register our site ID and Social application in our django admin First do migration & runserver
$(venv) python3 manage.py migrate
go to http://localhost:8000/admin/ and click site ID change example.com to http://localhost:8000 ( if you are already at production level , you might just use your page’s IP or domain.)
After saving the application, we are ready to login with facebook. Put these templates tags to top of a html where you want make your users to login.
{% load socialaccount %} {% providers_media_js %}
and you can write this exact position where you want to make login button
<a href="{% provider_login_url "facebook" method="js_sdk" %}">Login Button image</a>
This send your request to accounts/login page which handles your login procedures.
Reference: https://medium.com/@jinkwon711/django-allauth-facebook-login-b536444cbc6b
这篇关于调试AllAuth:尽管成功连接,社交帐户仍未记录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!