django-rest-auth registration/verify-email/ 不工作
django-rest-auth registration/verify-email/ not working
我刚从官方 django-rest-auth 站点下载演示并尝试使用,但一些 API 端点不工作。
我成功注册(注册)了 restful API 用户,我得到了响应密钥:
`{"key":"e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"}`
并且用户存在(当我登记数据库时)并且我也收到带有验证的电子邮件 link,但是当我尝试使用其他 api:
验证其电子邮件时
`curl -X POST http://127.0.0.1:9003/rest-auth/registration/verify-email/ -d "key=e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"`
我得到:
`{"detail":"Not found."}`
我哪里弄错了。这只是演示,我什么都没做,只是安装、设置发送电子邮件、主机和 运行 服务器。
此外,当我在电子邮件中单击 link 时,它会打开带有确认按钮的页面,当我单击确认时,我得到:
`Using the URLconf defined in demo.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^signup/$ [name='signup']
^email-verification/$ [name='email-verification']
^login/$ [name='login']
^logout/$ [name='logout']
^password-reset/$ [name='password-reset']
^password-reset/confirm/$ [name='password-reset-confirm']
^user-details/$ [name='user-details']
^password-change/$ [name='password-change']
^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm']
^rest-auth/
^rest-auth/registration/
^account/
^admin/
^accounts/profile/$ [name='profile-redirect']
^docs/$ [name='api_docs']
The current path, accounts/login/, didn't match any of these.`
为什么这也不起作用?这是我出错的演示?
请帮忙!
更新1:
这是settings.py文件:
`"""
Django settings for demo project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ma3c@7uu!%e0=tynp+i6+q%$)9v@$t(eulqurym_b=48z82&5n'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
# 'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'rest_framework_swagger',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'demo.urls'
WSGI_APPLICATION = 'demo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
# TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
REST_SESSION_LOGIN = True
#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'optional'
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
)
}
SWAGGER_SETTINGS = {
'LOGIN_URL': 'login',
'LOGOUT_URL': 'logout',
}
DEFAULT_FROM_EMAIL = 'xxxx@xxxx.com'
EMAIL_HOST = 'smtp.mail.xxxx.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxxx@xxxx.com'
EMAIL_HOST_PASSWORD = 'xxxxx'
EMAIL_USE_TLS = True`
这里是urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from rest_framework_swagger.views import get_swagger_view
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
name='signup'),
url(r'^email-verification/$',
TemplateView.as_view(template_name="email_verification.html"),
name='email-verification'),
url(r'^login/$', TemplateView.as_view(template_name="login.html"),
name='login'),
url(r'^logout/$', TemplateView.as_view(template_name="logout.html"),
name='logout'),
url(r'^password-reset/$',
TemplateView.as_view(template_name="password_reset.html"),
name='password-reset'),
url(r'^password-reset/confirm/$',
TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password-reset-confirm'),
url(r'^user-details/$',
TemplateView.as_view(template_name="user_details.html"),
name='user-details'),
url(r'^password-change/$',
TemplateView.as_view(template_name="password_change.html"),
name='password-change'),
# this url is used to generate email content
url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password_reset_confirm'),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^account/', include('allauth.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/profile/$', RedirectView.as_view(url='/', permanent=True), name='profile-redirect'),
url(r'^docs/$', get_swagger_view(title='API Docs'), name='api_docs')
]
更新2:
我想我找到了一些东西。在确认电子邮件中,我收到了 link,看起来像:
http://127.0.0.1:9003/account/confirm-email/MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8/
现在,如果我使用 MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8 作为休息的关键 api,我会得到成功响应。但是现在我不知道从 curl 响应中得到的密钥是什么,从确认电子邮件中得到的密钥是什么 link:
来自 curl 响应的密钥:e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe(此值位于 table authtoken_token
中的数据库中
来自确认电子邮件的密钥 link:MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8
请解释一下区别
我遇到了完全相同的问题,然后我只需添加以下设置:
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
我发现了什么是解决方案。这不是设置问题,而是我理解什么键放在哪里的问题。
您从 json api 响应 {"key":"e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"} 获得的密钥是您需要在 header 中使用的密钥每个 api 需要身份验证的调用。但是您在确认电子邮件 "MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8" 中收到的密钥是确认 link 的一部分,仅用于验证。相反,您可以点击电子邮件中的确认 link,您可以使用该部分(密钥)并使用 restful api 验证您的帐户。就这样。有两个不同的键。一个是身份验证密钥很重要,您始终需要保留它并将其与请求一起发送,以便服务器知道您已登录,第二个密钥仅用于验证帐户,您只在注册新帐户时使用一次并需要验证它。您可以通过单击确认电子邮件中的 link 来验证它,或者从 link 中获取密钥并通过验证 api 调用手动发送它以验证您的新帐户。
我刚从官方 django-rest-auth 站点下载演示并尝试使用,但一些 API 端点不工作。
我成功注册(注册)了 restful API 用户,我得到了响应密钥:
`{"key":"e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"}`
并且用户存在(当我登记数据库时)并且我也收到带有验证的电子邮件 link,但是当我尝试使用其他 api:
验证其电子邮件时 `curl -X POST http://127.0.0.1:9003/rest-auth/registration/verify-email/ -d "key=e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"`
我得到:
`{"detail":"Not found."}`
我哪里弄错了。这只是演示,我什么都没做,只是安装、设置发送电子邮件、主机和 运行 服务器。
此外,当我在电子邮件中单击 link 时,它会打开带有确认按钮的页面,当我单击确认时,我得到:
`Using the URLconf defined in demo.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^signup/$ [name='signup']
^email-verification/$ [name='email-verification']
^login/$ [name='login']
^logout/$ [name='logout']
^password-reset/$ [name='password-reset']
^password-reset/confirm/$ [name='password-reset-confirm']
^user-details/$ [name='user-details']
^password-change/$ [name='password-change']
^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm']
^rest-auth/
^rest-auth/registration/
^account/
^admin/
^accounts/profile/$ [name='profile-redirect']
^docs/$ [name='api_docs']
The current path, accounts/login/, didn't match any of these.`
为什么这也不起作用?这是我出错的演示?
请帮忙!
更新1:
这是settings.py文件:
`"""
Django settings for demo project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ma3c@7uu!%e0=tynp+i6+q%$)9v@$t(eulqurym_b=48z82&5n'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
# 'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'rest_framework_swagger',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'demo.urls'
WSGI_APPLICATION = 'demo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
# TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
REST_SESSION_LOGIN = True
#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'optional'
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
)
}
SWAGGER_SETTINGS = {
'LOGIN_URL': 'login',
'LOGOUT_URL': 'logout',
}
DEFAULT_FROM_EMAIL = 'xxxx@xxxx.com'
EMAIL_HOST = 'smtp.mail.xxxx.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxxx@xxxx.com'
EMAIL_HOST_PASSWORD = 'xxxxx'
EMAIL_USE_TLS = True`
这里是urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from rest_framework_swagger.views import get_swagger_view
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
name='signup'),
url(r'^email-verification/$',
TemplateView.as_view(template_name="email_verification.html"),
name='email-verification'),
url(r'^login/$', TemplateView.as_view(template_name="login.html"),
name='login'),
url(r'^logout/$', TemplateView.as_view(template_name="logout.html"),
name='logout'),
url(r'^password-reset/$',
TemplateView.as_view(template_name="password_reset.html"),
name='password-reset'),
url(r'^password-reset/confirm/$',
TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password-reset-confirm'),
url(r'^user-details/$',
TemplateView.as_view(template_name="user_details.html"),
name='user-details'),
url(r'^password-change/$',
TemplateView.as_view(template_name="password_change.html"),
name='password-change'),
# this url is used to generate email content
url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password_reset_confirm'),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^account/', include('allauth.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/profile/$', RedirectView.as_view(url='/', permanent=True), name='profile-redirect'),
url(r'^docs/$', get_swagger_view(title='API Docs'), name='api_docs')
]
更新2:
我想我找到了一些东西。在确认电子邮件中,我收到了 link,看起来像:
http://127.0.0.1:9003/account/confirm-email/MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8/
现在,如果我使用 MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8 作为休息的关键 api,我会得到成功响应。但是现在我不知道从 curl 响应中得到的密钥是什么,从确认电子邮件中得到的密钥是什么 link:
来自 curl 响应的密钥:e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe(此值位于 table authtoken_token
中的数据库中来自确认电子邮件的密钥 link:MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8
请解释一下区别
我遇到了完全相同的问题,然后我只需添加以下设置:
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
我发现了什么是解决方案。这不是设置问题,而是我理解什么键放在哪里的问题。
您从 json api 响应 {"key":"e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"} 获得的密钥是您需要在 header 中使用的密钥每个 api 需要身份验证的调用。但是您在确认电子邮件 "MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8" 中收到的密钥是确认 link 的一部分,仅用于验证。相反,您可以点击电子邮件中的确认 link,您可以使用该部分(密钥)并使用 restful api 验证您的帐户。就这样。有两个不同的键。一个是身份验证密钥很重要,您始终需要保留它并将其与请求一起发送,以便服务器知道您已登录,第二个密钥仅用于验证帐户,您只在注册新帐户时使用一次并需要验证它。您可以通过单击确认电子邮件中的 link 来验证它,或者从 link 中获取密钥并通过验证 api 调用手动发送它以验证您的新帐户。