Closed babor99 closed 1 year ago
Hi @babor99 I was trying to reproduce your error without any success - password authentication works for me in both scenarios. Could you provide a stack-trace of the error and urls.py config?
One more thing - I've noticed you are sending client_secret
hashed, in your POST request. The value should be the one you see when you create new application.
Exactly! @SukiCZ is absolutely right regarding your client secret. I am closing this issue because I also could not reproduce it successfully.
I am facing unsupported grant type (provided correct username, password, grant_type, client_id, client_secret) error when trying to get token. Tried with both curl and postman. I'm using python=3.10.2, django=3.2.4, djangorestframework=3.12.4, django-oauth-toolkit==2.1.0, drf-social-oauth2==1.2.1
I get this response (with grant_type: password): (venv) D:\someproject>curl -X POST -d "client_id=2aZsnx7Jcdg2beAJJNIYvChVgOuLY4O9dPcH3OkD&client_secret=pbkdf2_sha256$260000$Yu4SuLFINHg8VuI0JWfR94$yGQG0cGwendBeVmfUjh8JfhiFbTaAnF8D7sAC1gveH4=&grant_type=password&username=babor@gmail.com&password=babor" http://localhost:8000/auth/token {"error":"unsupported_grant_type"}
I get this response (with grant_type: client_credentials): (venv) D:\someproject>curl -X POST -d "client_id=2aZsnx7Jcdg2beAJJNIYvChVgOuLY4O9dPcH3OkD&client_secret=pbkdf2_sha256$260000$Yu4SuLFINHg8VuI0JWfR94$yGQG0cGwendBeVmfUjh8JfhiFbTaAnF8D7sAC1gveH4=&grant_type=client_credentials&username=babor@gmail.com&password=babor" http://localhost:8000/auth/token {"error":"unsupported_grant_type"}
NB: USERNAME_FIELD=email in the User model and here babor@gmail.com is a super user. Also changed the grant type in the django admin 'application' table to 'client_credentials' when sending the grant_type=client_credentials in the request. Nothing worked for me.
My custom user model is: class User(AbstractBaseUser): class Gender(models.TextChoices): MALE = 'male', ('Male') FEMALE = 'female', ('Female') OTHERS = 'others', _('Others') first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) username = models.CharField(max_length=100, unique=True, null=True, blank=True) email = models.EmailField(verbose_name='email address', max_length=255, unique=True, null=True, blank=True) user_type = models.CharField(max_length=50, null=True, blank=True)
settings.py:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
'order.apps.OrderConfig', 'purchase.apps.PurchaseConfig', 'purchase_final.apps.PurchaseFinalConfig', 'site_settings.apps.SiteSettingsConfig', 'support.apps.SupportConfig', 'report.apps.ReportConfig', 'dashboard.apps.DashboardConfig', ]
INSTALLED_APPS += ['sequences.apps.SequencesConfig']
MIDDLEWARE = [
Simplified static file serving.
]
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
ROOT_URLCONF = 'start_project.urls'
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, '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', 'social_django.context_processors.backends', 'social_django.context_processors.login_redirect', ], }, }, ]
WSGI_APPLICATION = 'start_project.wsgi.application'
AUTH_USER_MODEL = 'authentication.User'
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'some', 'USER': 'some', 'PASSWORD':'12345678' } }
AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # media files upload directory MEDIA_URL = '/media/' # media files retrieve directory
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
WHITENOISE_USE_FINDERS = True CORS_ALLOW_ALL_ORIGINS=True
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', # django-oauth-toolkit >= 1.0.0 'drf_social_oauth2.authentication.SocialAuthentication', ), 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', 'DEFAULT_PARSER_CLASSES': [ 'rest_framework.parsers.MultiPartParser', 'rest_framework.parsers.JSONParser', 'rest_framework.parsers.FormParser', ], 'DEFAULT_FILTER_BACKENDS': ( 'django_filters.rest_framework.DjangoFilterBackend', ), } AUTHENTICATION_BACKENDS = ( 'drf_social_oauth2.backends.DjangoOAuth2', 'django.contrib.auth.backends.ModelBackend', ) OAUTH2_PROVIDER = { 'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore', }
Desktop:
this is the application I created manually in django admin:![drf_social_oauth2_application](https://user-images.githubusercontent.com/47164803/184830706-1a88de24-7a98-4d8c-81e8-36e07adbe73a.PNG)