使用DOT的带有oAuth2的Django DRF(Djangoauthttoolkit)

2024-10-16 20:39:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使DRF与oAuth2(django oauth工具包)一起工作。

我在关注http://httplambda.com/a-rest-api-with-django-and-oauthw-authentication/

首先,我遵循了该指令,但随后,在获得身份验证错误后,我设置了此演示:https://github.com/felix-d/Django-Oauth-Toolkit-Python-Social-Auth-Integration

结果相同:我无法使用此curl生成访问令牌:

curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u "<client_id>:<client_secret>" http://127.0.0.1:8000/o/token/

我得到这个错误:

{"error": "unsupported_grant_type"}

oAuth2应用程序设置为grant_type password。我将grant_type更改为“client credentials”,并尝试了此curl:

curl -X POST -d "grant_type=client_credentials" -u "<client_id>:<client_secret>" http://127.0.0.1:8000/o/token/

这起作用,我得到了生成的身份验证令牌。

在那之后,我试着要一份所有啤酒的清单:

curl -H "Authorization: Bearer <auth_token>" http://127.0.0.1:8000/beers/

我得到的答复是:

{"detail":"You do not have permission to perform this action."}

这是views.py的内容,它应该显示啤酒:

from beers.models import Beer
from beers.serializer import BeerSerializer
from rest_framework import generics, permissions

class BeerList(generics.ListCreateAPIView):
    serializer_class = BeerSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        user = self.request.user
        return Beer.objects.filter(owner=user)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

我不知道这里有什么问题。首先是“未报告的授权类型”,然后是其他curl调用。当我在django oauth工具包中做基础教程时,这也发生在我身上。我用的是Django 1.8.2和python3.4

谢谢你的帮助!

我的settings.py看起来像这样

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

SECRET_KEY = 'hd#x!ysy@y+^*%i+klb)o0by!bh&7nu3uhg+5r0m=$3x$a!j@9'

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
)

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'oauth2_provider',
    'rest_framework',
    'beers',
)

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',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)
ROOT_URLCONF = 'beerstash.urls'

WSGI_APPLICATION = 'beerstash.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    )
}

OAUTH2_PROVIDER = {
    # this is the list of available scopes
    'SCOPES': {'read': 'Read scope', 'write': 'Write scope'}
}

Tags: djangoimportclientauthresttruehttptype