node-oauth2-server icon indicating copy to clipboard operation
node-oauth2-server copied to clipboard

Invalid or missing grant_type parameter

Open mike-aungsan opened this issue 11 years ago • 9 comments

I post these using Postman, grant_type, username, password, client_id and client_secret but having this error

{ "code": "BadMethod", "message": "Invalid or missing grant_type parameter" }

mike-aungsan avatar May 15 '15 01:05 mike-aungsan

@mike-aungsan Could you post the entire body of your Postman test? Can't really help very much without more information.

RobertHerhold avatar May 17 '15 15:05 RobertHerhold

@RobertHerhold

Here is Request Headers and form data. Many Thanks

Request Headers

Accept:/ Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Authorization:Basic ZXhhbXBsZS1jbGllbnQnOnBhc3N3b3Jk Cache-Control:no-cache Connection:keep-alive Content-Length:106 Content-Type:application/x-www-form-urlencoded DNT:1 Host:10.10.10.10 Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36

Form Data

view source view URL encoded grant_type:password username:[email protected] password:password client_id:papers3 client_secret:123

mike-aungsan avatar May 18 '15 23:05 mike-aungsan

@mike-aungsan In your app, what options are you passing in to the OAuth2Server() constructor? For example:

app.oauth = oauthServer({
    model: require('./auth'),
    grants: ['password'],
    debug: true
});

would allow you to use the password grant type. I would make sure you have password as one of the elements in the grants array. Also, in your grantTypeAllowed function, you should also make sure you are allowing password as one of the grants. For example:

auth.grantTypeAllowed = function(clientId, grantType, callback) {
    if (grantType === 'password') {
        callback(null, true);
    } else {
        callback('Invalid grant type!', false);
    }
};

Hope it helps.

RobertHerhold avatar May 19 '15 01:05 RobertHerhold

I had this problem and here is the part I missed:

app.use(bodyParser.urlencoded({ extended: true }));

Maybe it can help someone.

rgalite avatar Jun 30 '15 20:06 rgalite

Closing on no activity - please reopen if needed :)

thomseddon avatar Nov 20 '15 17:11 thomseddon

I am getting the same problem.

grant_type=auth_code&client_id=cliente&client_secret=segredo&username=usuario&password=senha

on the server console I get this "OAuth2Error: Invalid or missing grant_type parameter" I am using Postgres.

dev-intellisoft avatar Jun 14 '17 03:06 dev-intellisoft

@wellington-cunha It's grant_type=authorization_code, not grant_type=auth_code. See RFC.

Also, you're sending username/password which make no sense for that part of the authorization code flow. You probably wanted to use grant_type=password.

maxtruxa avatar Jun 14 '17 06:06 maxtruxa

I had an issue with the restify as I had something that I copy/pasted from the restify-oauth-server module :

server.use(function(req, res, next) {
  if(req.headers['content-type'] === 'application/x-www-url-formencoded') req.body = req.params;
  return next();
});

instead of :

  server.use(function(req, res, next) {
    if(req.headers['content-type'] === 'application/x-www-form-urlencoded') req.body = req.params;
    return next();
  });

which is correct according to the RFC : please check your content type HTTP header, and make sure to print the req.body.grant_type

Again according to the RFC for the password grant_type .. make sure to send the : client_id/client_secret in the Authorization header with the "Basic format"

For example if your Client_id is : "foo" and your password is "bar"

you will send the HTTP headers:

Authorization: Basic Zm9vOmJhcg== Conent-Type: application/x-www-form-urlencoded

ps : The Authorization Basic is the 64 encoding of the ClientID:Password (i.e. "foo:bar")

$.ajax({
   type: "POST",
  url: "https://www.example.com/api/token",
  dataType: 'json',
  headers: {
   'Content-Type': 'application/x-www-form-urlencoded',
   'Authorization', btoa("foo:bar")
   },
  data: '{
   "username":"james",
   "password":"bond",
   "grant_type":"password"
 }',
  success: function (res){
   ....
   }
  });

mgara avatar Jul 09 '17 05:07 mgara

I have some problem when I request by postman with grant_type: password return success but when I use React app with same parameter but always falid with message: {statusCode: 400, status: 400, code: 400, message: 'Missing parameter: grant_type', name: 'invalid_request' }

thanhbv2 avatar Jun 16 '18 07:06 thanhbv2