"Must specify grant_type field" 获取 Oauth2 令牌时出错

"Must specify grant_type field" error when getting Oauth2 token

我有一个简单的 node.js 后端构建在 RestifyOAuth2授权(restify-oauth2插件)。

我在请求令牌时遇到问题。当我用我的 REST 客户端调用 POST 方法时,一切正常,我得到 access_tokentoken_type.

当我尝试在 Angular.

中创建的前端应用程序中执行相同的操作时出现问题

这是我的代码:

 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');
 });

如您所见,提供了 grant_type 参数。但服务器仍然响应:

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

这是 devtools 截图:

我需要如何更改我的请求才能使其生效?

我终于解决了。问题是 grant_type 字段必须在请求正文中传递,而不是在参数中传递。

工作请求代码:

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');
});