【问题标题】:"Must specify grant_type field" error when getting Oauth2 token获取 Oauth2 令牌时出现“必须指定 grant_type 字段”错误
【发布时间】:2015-09-20 14:01:44
【问题描述】:

我在 Restify 上构建了一个简单的 node.js 后端,具有 OAuth2 授权(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 截图:

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

【问题讨论】:

    标签: angularjs node.js oauth-2.0 restify


    【解决方案1】:

    我终于解决了。问题是 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');
    });
    

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2021-10-18
      • 2012-07-14
      • 2022-01-23
      • 2018-11-03
      • 2020-12-17
      • 1970-01-01
      • 2020-07-08
      • 2020-10-09
      相关资源
      最近更新 更多