问题描述:

I've got a simple node.js backend build on Restify with OAuth2 authorization (restify-oauth2 plugin).

I've got a problem requesting a token. When I call POST method with my REST client, everything works correctly and I get access_token and token_type.

The problem occurs when I try to do the same thing in my frontend aplication made in Angular.

Here's my code:

 var req = {

method: 'POST',

url: 'http://localhost:8080/token',

headers: headers,

data: {},

params: {grant_type: "client_credentials"}

};

$http(req).success(function(data, status, headers, config){

console.log('success');

}).error(function(data, status, headers, config){

console.log('error');

});

So as you can see, grant_type parameter is provided. But still the server responds with:

{"error":"invalid_request","error_description":"Must specify grant_type field."}

And here's devtools screenshot:

How do I need to change my request to make it work?

网友答案:

I finally solved it. The thing is that grant_type field must be passed inside a request body, not in the parameters.

Working request code:

var req = {
  method: 'POST',
  url: 'http://localhost:8080/token',
  headers: headers,
  data: "grant_type=client_credentials"
};

$http(req).success(function(data, status, headers, config){
  console.log('success');
}).error(function(data, status, headers, config){
  console.log('error');
});
相关阅读:
Top