【发布时间】:2015-08-31 14:06:02
【问题描述】:
我正在尝试使 DRF 与 oAuth2 (django-oauth-toolkit) 一起使用。
我专注于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 密码。我将 grant_type 更改为“客户端凭据”并尝试了这个 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-toolkit 完成基本教程时,这也发生在我身上。我正在使用 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'}
}
【问题讨论】:
-
您的设置是什么样的?您是否添加了 oauth 的所有设置以用作 REST Framework API 身份验证?
-
@AlexT 我添加了我的 settings.py 文件
-
较新版本使用:
from oauth2_provider.contrib.rest_framework import OAuth2Authentication
标签: python django django-rest-framework