2

我只是從官方django-rest-auth網站下載演示,並試圖使用,但一些API端點不工作。django-rest-auth註冊/驗證電子郵件/不工作

我成功註冊(登記)與寧靜的API的用戶,我拿到鑰匙對應:

`{"key":"e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"}` 

和用戶存在(當我在數據庫中查詢),我還可以得到與驗證鏈接的電子郵件,但是當我嘗試驗證其電子郵件與REST API:

`curl -X POST http://127.0.0.1:9003/rest-auth/registration/verify-email/ -d "key=e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe"` 

我得到:

`{"detail":"Not found."}` 

其中ia m犯錯誤。這只是演示,我沒有做任何事情只是安裝,設置發送電子郵件,主機和運行服務器。

而且當我在電子郵件中的鏈接點擊打開頁面,確認按鈕,當我點擊確認我得到:

`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.` 

爲什麼這也不起作用?這是演示我犯了什麼錯誤?

請幫忙!

UPDATE1:

這裏是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 = '[email protected]!%e0=tynp+i6+q%$)[email protected]$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 = '[email protected]' 
EMAIL_HOST = 'smtp.mail.xxxx.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
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') 
] 

UPDATE2:

我想我找到了索姆ething。在確認電子郵件,我得到的鏈接看起來像:

http://127.0.0.1:9003/account/confirm-email/MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8/ 

現在如果我使用MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8在REST API我得到了成功響應的關鍵。但現在我不知道那是什麼,我從捲曲反應得到的,什麼是關鍵,我從確認電子郵件中的鏈接一鍵搞定:

從捲曲響應鍵:e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe(這個值存放在數據庫中的表authtoken_token

MQ:1d2Go5:從確認電子郵件中的鏈接

關鍵SHdLaJz9Pa1HluHw_Djr26jm3Q8

請給我解釋一下差異

+0

您是否在'urls.py'中添加了'rest_auth.registration.urls'? –

+0

是的,我做了網址(r'^ rest-auth/registration /',include('rest_auth.registration.urls')),畢竟這是正式的演示。註冊也適用於其他API,但驗證電子郵件不起作用。也許還有更多的設置? –

+0

你有沒有檢查過installed_apps設置? –

回答

0

我發現了什麼是解決方案。這不是問題在設置這是我的理解什麼鑰匙去哪裏問題。

您從響應{「key」:「e96496ecb7fbe85d5ab60fe5d5f9a15b33a967fe」}中得到的關鍵是您需要在標題中爲每個需要驗證的api調用使用的關鍵字。但是作爲確認鏈接的一部分的確認電子郵件「MQ:1d2Go5:SHdLaJz9Pa1HluHw_Djr26jm3Q8」得到的鑰匙僅用於驗證。相反,您點擊電子郵件中的確認鏈接,您可以取得該部分(關鍵),並通過restful api驗證您的帳戶。就這樣。有兩個不同的鍵。一個是認證密鑰是重要的,你總是需要保留它併發送請求,所以服務器知道你已經登錄,第二個密鑰僅用於驗證帳戶,並且當你註冊新帳戶時只需要使用它一次,需要驗證它。您可以通過單擊確認電子郵件中的鏈接對其進行驗證,或者從該鏈接獲取密鑰並手動將其與驗證API調用發送以驗證您的新帳戶。

+0

如何更改電子郵件中發送的網址?那個設置在哪裏? –

+0

您不需要更改該URL。該URL由django自動生成,並通過帳戶電子郵件發送,需要進行驗證。如果你想要,你可以用不同的文本更改電子郵件模板,但會生成網址。 –

0

我有完全相同的問題,那麼我根本就添加以下設置:

ACCOUNT_EMAIL_VERIFICATION = 'mandatory' ACCOUNT_EMAIL_REQUIRED = True

相關問題