我试图让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密码设置的。我将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工具包的基础教程时,我也遇到了这种情况。我使用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'}
}发布于 2015-06-20 14:08:42
我试过你提到的演示,一切都很好。
$ curl -X POST -d "grant_type=password&username=superuser&assword=123qwe" -u"xLJuHBcdgJHNuahvER9pgqSf6vcrlbkhCr75hTCZ:nv9gzOj0BMf2cdxoxsnYZuRYTK5QwpKWiZc7USuJpm11DNtSE9X6Ob9KaVTKaQqeyQZh4KF3oZS4IJ7o9n4amzfqKJnoL7a2tYQiWgtYPSQpY6VKFjEazcqSacqTx9z8" http://127.0.0.1:8000/o/token/
{"access_token": "jlLpKwzReB6maEnjuJrk2HxE4RHbiA", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "DsDWz1LiSZ3bd7NVuLIp7Dkj6pbse1", "scope": "read write groups"}
$ curl -H "Authorization: Bearer jlLpKwzReB6maEnjuJrk2HxE4RHbiA" http://127.0.0.1:8000/beers/
[]在您的例子中,我认为您创建了一个错误的“授权授予类型”应用程序。
使用此应用程序设置:
Name: just a name of your choice
Client Type: confidential
Authorization Grant Type: Resource owner password-based这个started.html#step-3-register-an-application帮了我很多忙。
这里是我创建的数据库文件:https://www.dropbox.com/s/pxeyphkiy141i1l/db.sqlite3.tar.gz?dl=0
你可以自己试试。根本没有修改源代码。Django管理用户名-超级用户,密码-123。
发布于 2016-02-17 14:39:04
当您使用“客户端凭据”时,它不会在生成的访问令牌上设置用户,这是您所看到的you do not have permission错误的根源。
当使用client credentials授予类型时,需要设置Rest权限处理程序来查看令牌,因为client credentials没有在生成的令牌上设置用户。Django OAuth工具包为此目的提供了自定义权限:
https://django-oauth-toolkit.readthedocs.org/en/latest/rest-framework/permissions.html
或者,如果整个API受相同类型的权限约束,则只需在settings.py文件中全局设置权限处理程序,例如:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.ext.rest_framework.OAuth2Authentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'oauth2_provider.ext.rest_framework.TokenHasReadWriteScope',
)
}当然,这假定您当时授予了read write权限。
有关作用域的更多信息,请参见:
https://django-oauth-toolkit.readthedocs.org/en/latest/settings.html
https://stackoverflow.com/questions/30855991
复制相似问题