Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS Api网关:NodeJs post请求-“;“未配置授权”;关于cognito登录用户_Node.js_Https_Aws Api Gateway_Amazon Cognito - Fatal编程技术网

Node.js AWS Api网关:NodeJs post请求-“;“未配置授权”;关于cognito登录用户

Node.js AWS Api网关:NodeJs post请求-“;“未配置授权”;关于cognito登录用户,node.js,https,aws-api-gateway,amazon-cognito,Node.js,Https,Aws Api Gateway,Amazon Cognito,我一直在尝试使用POST访问我的lambda,登录时使用Nodejs 我得到一个错误: 未配置授权 我正在使用登录成功时捕获的ID令牌。我将我的cognito帐户作为网关配置中的授权资源。我的令牌来源是“授权” 我的代码是: cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { var accessToken = result.getAccessToken(

我一直在尝试使用POST访问我的lambda,登录时使用Nodejs

我得到一个错误:

未配置授权

我正在使用登录成功时捕获的ID令牌。我将我的cognito帐户作为网关配置中的授权资源。我的令牌来源是“授权”

我的代码是:

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        var accessToken = result.getAccessToken().getJwtToken();
        var authToken = result.getIdToken().getJwtToken();
        console.log("Logged in");
        requestSignature(authToken);
    },  

    onFailure: function(err) {
        console.log(err)
        authToken = ''
    },  

});

function requestSignature(authToken) {

    var request = require('request');

    headers = { 
            Authorization: authToken,
    }   
    data = { 
        key : "key",
        bucket : "test"
    }   

    var options = { 
        method: 'POST',
        uri: invoke_url+'/getsignature',
        contentType: 'application/json',
        headers: headers
        data: data
    };

    request.post(options,(err, response, body) => {
        if (response.statusCode < 300) {
            return callback(null, {
                statusCode: '200',
                body: res,
                headers: { 'Content-Type': 'application/json' },
            });
        } else {
            console.log(response)
        }
    });
}
cognitoUser.authenticateUser(authenticationDetails{
onSuccess:函数(结果){
var accessToken=result.getAccessToken().getJwtToken();
var authToken=result.getIdToken().getJwtToken();
控制台日志(“登录”);
请求签名(authToken);
},  
onFailure:函数(err){
console.log(错误)
authToken=''
},  
});
函数requestSignature(authToken){
var请求=要求(“请求”);
标题={
授权:authToken,
}   
数据={
钥匙:“钥匙”,
桶:“测试”
}   
变量选项={
方法:“POST”,
uri:invoke_url+'/getsignature',
contentType:'应用程序/json',
标题:标题
数据:数据
};
请求.发布(选项,(错误,响应,正文)=>{
如果(response.statusCode<300){
返回回调(null{
状态代码:“200”,
正文:res,
标题:{'Content-Type':'application/json'},
});
}否则{
console.log(响应)
}
});
}

问题具体出现在API网关测试实用程序中

在我的lambda函数中

if (!event.requestContext.authorizer) {
      errorResponse('Authorization not configured', context.awsRequestId, callback);
      return;
    }
由于无法将授权标头传递到方法测试实用程序中,并且由于某些原因无法获取cognito ID,因此被绊倒

当我删除那行时,我收到了一个成功的回复。当我返回传递到lambda的事件时,我在requestContext中没有收到任何“authorizer”

这使我相信授权没有正常运行。我完全删除了授权头和令牌,并在相同的requestContext下收到了相同的成功lambda响应

返回应用程序时,当我没有authorizationId令牌时,我收到此错误:

'{"message":"Unauthorized"}'
当我添加授权时,我在请求上下文中收到一个成功的Lambda请求:

"authorizer":
  {"claims":
    {"sub":"ba<>",
     "aud":"4v<>",
     "email_verified":"true",
     "event_id":"<>",
     "token_use":"id",
     "auth_time":"1530899925",
     "iss":"https://cognito-idp.<region>.amazonaws.com/<cognito-id>",
     "cognito:username":"tester",
     "exp":"<>",
     "iat":"<>", 
     "email":"<>@<>"}},
     "resourcePath":"/<>",
     "httpMethod":"POST",
     "extendedRequestId":"<>",
     "requestTime":"<>",
     "path":"<>",
     "accountId":"<>",
“授权人”:
{“索赔”:
{“sub”:“ba”,
“澳元”:“4v”,
“电子邮件已验证”:“正确”,
“事件id”:“,
“令牌使用”:“id”,
“授权时间”:“1530899925”,
“国际空间站”:https://cognito-idp..amazonaws.com/",
“cognito:username”:“tester”,
“exp”:“exp”,
“iat”:“,
“电子邮件”:“@”},
“资源路径”:“/”,
“httpMethod”:“POST”,
“extendedRequestId”:“”,
“请求时间”:“,
“路径”:“,
“accountId”:“,
我的应用程序需要授权,并返回相应的上下文

然后,Cognito授权的问题完全在API网关测试中解决