我在制作中设置了social-auth-app-django
。但在facebook重定向后,我得到了AuthCanceled
,仍然无法让它工作。
在我的用户模型中用于注册的电子邮件地址。这是我的用户模型:
class User(AbstractUser):
email = models.EmailField(_('email address'), unique=True)
avatar = models.ImageField(blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
在settings.py中:
MIDDLEWARE = [
...
'social_django.middleware.SocialAuthExceptionMiddleware',
]
TEMPLATES = [
...
'context_processors': [
...
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.facebook.FacebookOAuth2',
'accounts.backends.ModelBackend'
)
LOGIN_URL = '/'
LOGOUT_URL = '/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
SOCIAL_AUTH_FACEBOOK_KEY = '..' # App ID
SOCIAL_AUTH_FACEBOOK_SECRET = '...' # App Secret
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'id,name,email',
}
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)
Facebook登录应用中的有效OAuth重定向URI:https://example.com/oauth/complete/facebook/
感谢您的时间和帮助。
对于那些登陆这个问题的人,在通过电子邮件与OP讨论这个话题之后,问题是由于redirect_uri
交换code
时access_token
的错误构建造成的。
这个URI的错误构建是因为Django后端位于Nginx后面,但是没有传递给它的正确的头文件(X-Forwarded-For
和Host
),所以Django没有意识到它是服务请求的正确主机,最后,网址指向localhost
而不是预期的域。
对我来说,一切都像魅力一样:Facebook,谷歌,Twitter和github。这可能是配置问题。
这是我如何设置所有内容(注意:我在Facebook中处于开发者模式,但它与我的2个帐户一起工作正常),我希望这可以帮助:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social_django',
'compressor',
'app',
)
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
)
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.github.GithubOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
# https://simpleisbetterthancomplex.com/
# tutorial/2016/10/24/how-to-add-social-login-to-django.html
SOCIAL_AUTH_GITHUB_KEY = 'xxx'
SOCIAL_AUTH_GITHUB_SECRET = 'xxx'
SOCIAL_AUTH_TWITTER_KEY = 'xxx'
SOCIAL_AUTH_TWITTER_SECRET = 'xx'
SOCIAL_AUTH_FACEBOOK_KEY = 'xxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxx'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'name, email, age_range'
}
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'xxx'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'xxx'