Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported_grant_type #56

Closed
rodolphopivetta opened this issue May 20, 2016 · 14 comments
Closed

unsupported_grant_type #56

rodolphopivetta opened this issue May 20, 2016 · 14 comments

Comments

@rodolphopivetta
Copy link

I'm trying to authenticate an user following the instructions in the README, but when I request a token for an user, I receive:

{"error": "unsupported_grant_type"}

In my application the "Authorization grant type" field is "password" value:

In [1]: from oauth2_provider.models import Application

In [2]: a = Application.objects.get(id=1)

In [11]: a.authorization_grant_type
Out[11]: u'password'

What I'm doing wrong?

@PhilipGarnero
Copy link
Collaborator

Show me your request so I can help you out

@rodolphopivetta
Copy link
Author

using the URL

http://127.0.0.1:8000/auth/token/

method: POST

Body:

{
    "grant_type": "password",
    "client_id": "SxOKjAuWdHDw07NIyogYhjOTn6amDVXSG9WjYydn",
    "client_secret": "ejvOxEmloOobtB45USCljfASygtXxpOUbQiWxJwE60RLA7rhTSmWNLi3YonCTj5orKq8JDxyiKa66QP5mAoiu8mu57qJeCPmXv689P3PoSeJcb7Va30wI8dyJjSrazMR",
    "username": "testuser",
    "password": 123
}

@PhilipGarnero
Copy link
Collaborator

You are sending an integer instead of a string for the password.

@rodolphopivetta
Copy link
Author

Same error as string

{
    "grant_type": "password",
    "client_id": "SxOKjAuWdHDw07NIyogYhjOTn6amDVXSG9WjYydn",
    "client_secret": "ejvOxEmloOobtB45USCljfASygtXxpOUbQiWxJwE60RLA7rhTSmWNLi3YonCTj5orKq8JDxyiKa66QP5mAoiu8mu57qJeCPmXv689P3PoSeJcb7Va30wI8dyJjSrazMR",
    "username": "testuser",
    "password": "123"
}

@rodolphopivetta
Copy link
Author

In [1]: from oauth2_provider.models import Application

In [2]: a = Application.objects.get(id=1)

In [3]: a.client_id
Out[3]: u'SxOKjAuWdHDw07NIyogYhjOTn6amDVXSG9WjYydn'

In [4]: a.client_secret
Out[4]: u'ejvOxEmloOobtB45USCljfASygtXxpOUbQiWxJwE60RLA7rhTSmWNLi3YonCTj5orKq8JDxyiKa66QP5mAoiu8mu57qJeCPmXv689P3PoSeJcb7Va30wI8dyJjSrazMR'

In [5]: a.client_type
Out[5]: u'confidential'

In [6]: a.authorization_grant_type
Out[6]: u'password'

In [7]: from users.models import CommonUser

In [8]: c = CommonUser.objects.all().first()

In [9]: c.set_password("123")

In [10]: c.username
Out[10]: u'roger_freeman'

In [11]: c.save()

In postman:

URL: http://127.0.0.1:8000/auth/token/

Method: POST

{
    "client_id": "SxOKjAuWdHDw07NIyogYhjOTn6amDVXSG9WjYydn",
    "client_secret": "ejvOxEmloOobtB45USCljfASygtXxpOUbQiWxJwE60RLA7rhTSmWNLi3YonCTj5orKq8JDxyiKa66QP5mAoiu8mu57qJeCPmXv689P3PoSeJcb7Va30wI8dyJjSrazMR",
    "username": "roger_freeman",
    "password": "123",
    "grant_type": "password"
}

Response:

{
  "error": "unsupported_grant_type"
}

@PhilipGarnero
Copy link
Collaborator

Is your Application object set to confidential ?

@rodolphopivetta
Copy link
Author

Yes, you can see this in the iPython query above

@rodolphopivetta
Copy link
Author

In [1]: from oauth2_provider.models import Application

In [2]: a = Application.objects.get(id=1)

In [5]: a.client_type
Out[5]: u'confidential'

😄

@rodolphopivetta
Copy link
Author

I discovered the problem, I'm sending requests with Content-Type: application/json, but apparently the required is Content-Type: application/x-www-form-urlencoded

Why is required x-www-form-urlencoded and not json?

And I suggest you add this requirement in the README 😄

@PhilipGarnero
Copy link
Collaborator

I had no idea.
It's probably coming from the oauth toolkit package.

@rodolphopivetta
Copy link
Author

I finally resolve my problem adding this in the settings:

OAUTH2_PROVIDER = {
    'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',
}

Thanks for your attention @PhilipGarnero and consider adding this information in the instructions

@earmc
Copy link

earmc commented Oct 13, 2016

good night everbody. I whould like know if someone have problem with made refresh token. I have this answer "unsupported_grant_type" but when i try to post request a refresh token. I appreciate if someone can suggest me some solution

this is my body request:

RequestBody requestBody = new FormEncodingBuilder()

            .add("client_id","confidencial")
            .add("client_secret","confidencial")
            .add("grant_type", "refresh_token")
            .add("refresh_token", tokenRefreshData)
            .add("Content-Type", "application/x-www-form-urlencoded")
            .build();

@yannbu
Copy link

yannbu commented Oct 9, 2017

@earmc I have the same problem. Did you figure it out ? If yes, how ? Thanks!

@hustzoujia
Copy link

@rodolphopivetta thanks for your right answer. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants