JavaScript AWS Client Gateway API x-api-key问题

时间:2019-03-12 00:13:09

标签: node.js amazon-web-services aws-sdk aws-api-gateway

我们正在使用NodeJS服务器上的JavaScript AWS AWS Client Gateway API调用AWS REST API。我们有私有的vpc发布的API,要求在API标头中输入标头。这是代码。 我在下面的脚本中有用于测试目的的硬编码API密钥。

    var apigClientFactory = require('aws-api-gateway-client').default;
let awsbody = JSON.parse(process.argv[2].split('\\').join('') || '{}');

var apigClient = apigClientFactory.newClient({
    invokeUrl: awsbody.endPoint, // REQUIRED
    accessKey: awsbody.awsAccessKey, // REQUIRED
    secretKey: awsbody.awsSecreteKey, // REQUIRED
    region: awsbody.awsRegion,
    apiKey: 'vs2i50xvo4',
    retryCondition: (err) => { 
        return err.response && err.response.status === 500;
    }
});

var pathParams = awsbody.awsPathParams;

var pathTemplate = awsbody.awsPathTemplate; // '/api/v1/sites'
var method = awsbody.method; // 'POST';
var additionalParams = { queryParams: awsbody.awsAdditionalParams }; //queryParams if any
//var additionalParams = {};

var body = awsbody.requestBody;
var output = {};


apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
    .then(function(result) {
        // return JSON.parse(result.data);
        output.status = result.response.data.status,
            output.message = result.response.data.message,
            output.responsecode = result.response.status,
            output.responsetext = result.response.statusText;
        output.header = result.response.headers;
        console.log(output)

    }).catch(function(result) {

        output.status = result.response.data.status,
            output.message = result.response.data.message,
            output.responsecode = result.response.status,
            output.responsetext = result.response.statusText,
            output.header = result.response.headers;
        console.log(output)
            //console.log("********** Output **************")
            //console.log(result)
            //return result;
    });

尝试执行代码时,出现以下错误。不知道上面的代码中是否有任何错误。

{ status: undefined,
  message: 'Forbidden',
  responsecode: 403,
  responsetext: 'Forbidden',
  header:
   { server: 'Server',
     date: 'Tue, 12 Mar 2019 00:00:37 GMT',
     'content-type': 'application/json',
     'content-length': '24',
     'x-amzn-requestid': 'ddce6604-4459-11e9-a23b-d1e9d6814b3a',
     'x-amzn-errortype': 'ForbiddenException',
     'x-amz-apigw-id': 'WZt53GORPHcFebA=',
     connection: 'close' } }

0 个答案:

没有答案