2017-06-21 72 views
3

我想爲我的Django REST API(使用DjangoRestFramework和Django-Oauth-Toolkit)設置OAuth2身份驗證系統。 我根據官方文檔寫的一切,但系統給出的錯誤「無法導入ext.rest_framework」Django OAuth工具包:無法導入ext.rest_framework

這裏是我的setting.py文件:

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


REST_FRAMEWORK = { 
    'DEFAULT_PERMISSION_CLASSES': [ 
     'oauth2_provider.ext.rest_framework.OAuth2Authentication', 
    ], 
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',), 
    'PAGE_SIZE': 10 
} 

謝謝!

回答

6

好的,我檢查了oauth2_provider的源代碼。顯然他們改變了結構,但沒有更新他們網站上的教程。因此,oauth2_provider.ext包不再存在,您應該使用oauth2_provider.contrib來代替。也就是說,下面的代碼工作正常:

REST_FRAMEWORK = { 
    'DEFAULT_AUTHENTICATION_CLASSES': (
     'oauth2_provider.contrib.rest_framework.OAuth2Authentication', 
    ), 
    'DEFAULT_PERMISSION_CLASSES': (
     'rest_framework.permissions.IsAuthenticated', 
    ), 
    'PAGE_SIZE': 10 
}