【问题标题】:Invoke AWS REST API in java-script在 java 脚本中调用 AWS REST API
【发布时间】:2019-12-06 10:48:28
【问题描述】:

我正在尝试使用 nodejs (aws-sdk) 执行 AWS Endpoint。首先,我能够为有权执行 API 的服务帐户生成会话令牌。

var AWS = require('aws-sdk');
AWS.config.update({ "accessKeyId": "<>", "secretAccessKey": "<>", "region": "us-west" });
var sts = new AWS.STS();
var response = {};
sts.assumeRole({
    RoleArn: 'arn:aws:iam::170000000000:role/service-account',
    RoleSessionName: 'AssumtaseRole'
}, function(err, data) {
    if (err) { // an error occurred
        var error = {}
        response.message = err.originalError.message,
            response.errno = err.originalError.errno,
            response.code = 404;
        console.log(response);
    } else { // successful response
        response.code = 200,
            response.accesskey = data.Credentials.AccessKeyId,
            response.secretkey = data.Credentials.SecretAccessKey,
            response.sessiontoken = data.Credentials.SessionToken,
            console.log(response);
    }
});

现在我正在尝试使用上述会话令牌执行端点。如果使用邮递员测试会话令牌,我可以执行 API,但不确定如何使用 (aws-sdk) 或 ('aws-api-gateway-client')

我尝试使用简单的 HTTPPS 请求执行,但出现错误:这是代码:

var AWS = require('aws-sdk');
var apigClientFactory = require('aws-api-gateway-client').default;

AWS.config.update({ "accessKeyId": "<>", "secretAccessKey": "<>", "region": "us-west" });

var sts = new AWS.STS();
var response = {};
sts.assumeRole({
    RoleArn: 'arn:aws:iam::170000000000:role/service_account',
    RoleSessionName: 'AssumtaseRole'
}, function(err, data) {
    if (err) { // an error occurred
        var error = {}
        response.message = err.originalError.message,
            response.errno = err.originalError.errno,
            response.code = 404;
        console.log(response);
    } else { // successful response
        response.code = 200,
            response.accesskey = data.Credentials.AccessKeyId,
            response.secretkey = data.Credentials.SecretAccessKey,
            response.sessiontoken = data.Credentials.SessionToken,
            console.log(response);
        var apigClient = apigClientFactory.newClient({
            invokeUrl: "https://some-endpoint.com", // REQUIRED
            accessKey: data.Credentials.AccessKeyId, // REQUIRED
            secretKey: data.Credentials.SecretAccessKey, // REQUIRED
            sessiontoken: data.Credentials.SessionToken,
            region: "us-west", // REQUIRED: The region where the AapiKeyloyed.
            retries: 4,
            retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried.  Uses axon-retry plugin.
                return err.response && err.response.status === 500;

            }
        });

        var pathParams = "";
        var pathTemplate = "/agent/registration"; // '/api/v1/sites'
        var method = "post"; // 'POST';
        var additionalParams = ""; //queryParams & Headers if any

        var body = {
            "agent_number": "1200",
            "agent_name": "Test"
        };

        apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
            .then(function(result) {
                console.log(result)

            }).catch(function(error) {
                console.log(error)

            });
        // console.log(output);

    }
});

这是错误:

     data:
      { message: 'The security token included in the request is invalid.' } } }

提前致谢。

谢谢基兰

【问题讨论】:

  • 我猜你可以使用为 nodejs 提供的任何 http 库,不需要是特定于 aws 的。
  • 请举个例子。
  • 我的意思是,如果你可以用邮递员做,那么你可以用节点中的request(或类似的)做同样的事情
  • 就像@Bergi 所说,您应该能够向端点发送 HTTP 请求。一个可以提供帮助的简单库是 axios
  • @bergi & mrstack99 感谢您的快速回复。我已经更新了原帖。

标签: javascript aws-sdk aws-sdk-js aws-sdk-nodejs


【解决方案1】:

请将sessiontoken 更改为sessionToken。这将解决您的问题。我已经在我的机器上测试了代码。

当我使用sessiontoken 进行测试时,我也收到了错误The security token included in the request is invalid.。当我将其更改为正确的键 sessionToken 时,它起作用了。

这里是简化的代码。当我测试时,我有硬编码的 accessKey、secretKey 和 sessionToken。

var apigClientFactory = require('aws-api-gateway-client').default;
var apigClient = apigClientFactory.newClient({
    invokeUrl:'https://api-url.com', // REQUIRED
    accessKey: '', // REQUIRED
    secretKey: '', // REQUIRED
    sessionToken: '', //OPTIONAL: If you are using temporary credentials you must include the session token
    region: 'ap-southeast-2', // REQUIRED: The region where the API is deployed.
    systemClockOffset: 0, // OPTIONAL: An offset value in milliseconds to apply to signing time
    retries: 4, // OPTIONAL: Number of times to retry before failing. Uses axon-retry plugin.
    retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried.  Uses axon-retry plugin.
      return err.response && err.response.status === 500;
    }
});


(() => {
  apigClient.invokeApi(null, `/hello`, 'GET')
  .then(function(result){
    console.log('result: ', result)
      //This is where you would put a success callback
  }).catch( function(result){
    console.log('result: ', result)
      //This is where you would put an error callback
  });
})()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多