首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java-script中调用AWS REST API

在java-script中调用AWS REST API
EN

Stack Overflow用户
提问于 2019-12-06 07:52:20
回答 1查看 356关注 0票数 1

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

代码语言:javascript
复制
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);
    }
});

现在,我正在尝试使用上面的会话令牌来执行端点。如果使用postman测试会话令牌,我可以执行API,但不确定如何使用(aws-sdk)或(‘aws-api-gateway client’)

我尝试使用简单的HTPPS请求执行,但得到错误:以下是代码:

代码语言:javascript
复制
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);

    }
});

下面是错误:

代码语言:javascript
复制
     data:
      { message: 'The security token included in the request is invalid.' } } }

提前谢谢。

谢谢你,基兰

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-06 10:30:27

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

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

以下是简化的代码。当我测试的时候,我已经硬编码了accessKey、secretKey和sessionToken。

代码语言:javascript
复制
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
  });
})()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59204907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档