AWS API网关授权失败

问题描述:

我已将AWS API网关与AWS Cognito用户池集成。以下curl命令起作用。AWS API网关授权失败

curl -X PUT -H "Authorization:<id token>" -d <json data> <https url> 

但是,以下Angularjs $ http.put授权失败。 AWS CloudWatch的日志显示“用户是未经授权的”

$http.defaults.headers.common['Authorization'] = token 
$http.put('https://<url>', <json data>) 
    .then(function (data, status, headers, config) { 
     console.log('success') 
    }, function (data, status, headers, config) { 
     console.log('failure') 
    }); 

应该如何授权令牌可以用$ http.put设置?

我认为你想要做的是将请求头设置为$http.put(<url>, options)...中的第二个参数。

所以你的情况,你的代码应该是这样的:

var options = {headers: { 
         'Authorization': token, 
         'Content-Type': 'application/json' 
         } 
       }; 

$http.put('https://<url>', <json data>, options); 

您可以检查documentation here了解更多关于调用$http.X方法的不同方式。

我希望这会有所帮助。