简体   繁体   English

在 java 脚本中调用 AWS REST API

[英]Invoke AWS REST API in java-script

I am trying to execute AWS Endpoint using nodejs (aws-sdk).我正在尝试使用 nodejs (aws-sdk) 执行 AWS Endpoint。 First, I am able to generate session token for Service Account which has access to execute the API.首先,我能够为有权执行 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); } });

Now I am trying to execute the endpoint using the above session token.现在我正在尝试使用上述会话令牌执行端点。 If test session token using postman, I am able to execute the API but not sure how to do it using (aws-sdk) or ('aws-api-gateway-client')如果使用邮递员测试会话令牌,我可以执行 API,但不确定如何使用 (aws-sdk) 或 ('aws-api-gateway-client')

I tried to execute using simple HTPPS request but getting error: Here is the code:我尝试使用简单的 HTPPS 请求执行,但出现错误:这是代码:

 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); } });

Here is the error:这是错误:

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

Thanks in advance.提前致谢。

Thank You Kiran谢谢基兰

Please change sessiontoken to sessionToken .请将sessiontoken更改为sessionToken that will fix your issue.这将解决您的问题。 I have tested the code on my machine.我已经在我的机器上测试了代码。

When i tested with sessiontoken i also received the error The security token included in the request is invalid.当我使用sessiontoken进行测试时,我还收到错误The security token included in the request is invalid. . . It worked when i changed it to the correct key which is sessionToken .当我将其更改为正确的密钥sessionToken时,它起作用了。

here is simplified code.这是简化的代码。 When i tested, I have hard coded accessKey, secretKey and 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
  });
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM