Django社交认证错误

时间:2015-10-26 14:30:11

标签: python django django-socialauth python-social-auth

当我尝试在Django上使用Google登录时,我收到此错误:

NoReverseMatch at /social/complete/google-oauth2/

Reverse for 'authorize' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method:     GET
Request URL:    http://127.0.0.1:8000/social/complete/google-oauth2/?state=GjvbQRt2HA8321312lVJTAGAdgUMbV&code=4/dRm-4hiTjN4PXB7P312312321d5DEyhGgsu9tG4Ik
Django Version:     1.8.2
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'authorize' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 496
    Python Executable:  /usr/bin/python
Python Version:     2.7.6</pre>

我的django设置是:

    'oauth2_provider',
    'social.apps.django_app.default',
    'rest_framework_social_oauth2',
)

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',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'Cofipy.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'message/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',
                'friends.context_processors.friends_requests',
                'cofipy_app.context_processors.ideea',
                'social.apps.django_app.context_processors.backends',
                'social.apps.django_app.context_processors.login_redirect',
            ],
        },
    },
]



REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        # OAuth
    'oauth2_provider.ext.rest_framework.OAuth2Authentication',
        'rest_framework_social_oauth2.authentication.SocialAuthentication',
    )
}


AUTHENTICATION_BACKENDS = (

    # Others auth providers (e.g. Google, OpenId, etc)

    'social.backends.google.GoogleOAuth2', 

    # Facebook OAuth2
    'social.backends.facebook.FacebookAppOAuth2',
    'social.backends.facebook.FacebookOAuth2',

    # django-rest-framework-social-oauth2
    'rest_framework_social_oauth2.backends.DjangoOAuth2',

    # Django
    'django.contrib.auth.backends.ModelBackend',

)

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
)

LOGIN_REDIRECT_URL = "/profile"

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'xxxx'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'xxxx'

# Google OAuth2 (google-oauth2)
SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile'
]

SOCIAL_AUTH_FACEBOOK_KEY = 'xxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxx'

urls.py:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework')),
    url(r'^', include("cofipy_app.urls")),
    url(r'^', include("friends.urls")),
    url(r'^messages/', include("message.urls")),
    url(r'^userprofile/', include("user_profile.urls")),
    url(r'^profile/', 'cofipy_app.views.profile'),
    url(r'^', include('social.apps.django_app.urls', namespace='social')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我试图使用以下方式访问链接:
a href =“{%url'social:begin''google-oauth2'%}”

我会提供任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我刚刚找到解决方案:
如果您使用django-rest-framework-social-oauth2,则必须在url文件中放置两个网址,否则您将收到此错误:

url(r'^social/', include('social.apps.django_app.urls', namespace='social')),
url(r'^auth/', include('rest_framework_social_oauth2.urls')),

由于