IncompleteSignatureException访问API AWS

时间:2018-08-30 12:08:28

标签: reactjs amazon-web-services api aws-sdk

我正在开发一个React Web应用程序,该应用程序必须连接到AWS API Pricing。为此,我像这样添加了节点软件包aws-sdk:

package.json

...
"dependencies": {
    "@material-ui/core": "^1.5.0",
    "@material-ui/icons": "^2.0.2",
    "aws-sdk": "^2.305.0",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-scripts": "1.1.4"
  }
...

这是我要访问AWS API的组件

import React, { Component } from "react";
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardActions from "@material-ui/core/CardActions";
import CardContent from "@material-ui/core/CardContent";
import CardHeader from "@material-ui/core/CardHeader";
import Divider from "@material-ui/core/Divider";
import Grid from "@material-ui/core/Grid";
import SelectField from "../SelectField";
import { withStyles } from "@material-ui/core/styles";
import styles from "../StylesProvider";
import AWS from "aws-sdk";

class AWSProvider extends Component {
  state = {
    vcpu: "",
    memory: "",
    instanceFamily: "",
    operatingSystem: "",
    networkPerformance: "",
    location: "",
    vcpusValues: ""
  };

  componentDidMount() {
    const creds = new AWS.Credentials('aws_access_key_id', 'aws_secret_access_key');
    var ep = new AWS.Endpoint('api.pricing.ap-south-1.amazonaws.com');


    const config = new AWS.Config();
    config.credentials = creds;
    config.apiVersion = '2017-10-15';
    config.region = "ap-south-1 ";

    var pricing = new AWS.Pricing();
    pricing.config = config;
    pricing.endpoint = ep;
    console.log(pricing);
    debugger;

    var params = {
      AttributeName: "volumeType",
      MaxResults: 2,
      ServiceCode: "AmazonEC2"
    };
    pricing.getAttributeValues(params, function (err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else console.log(data);           // successful response

    });
  }

  handleChange = event => {
    this.setState({ [event.target.name]: event.target.value });
  };

  render() {
    const { classes } = this.props;
    return (
      <Grid item>
        <Card>
          <CardHeader title={"Amazon Web Services"} />
          <Divider />
          <CardContent>
            <form className={classes.form} autoComplete="off">
              ...
            </form>
          </CardContent>
          <Divider />
          <CardActions>
            <Button variant="outlined" size="medium" color="primary">
              Buscar
            </Button>
          </CardActions>
        </Card>
      </Grid>
    );
  }
}

export default withStyles(styles)(AWSProvider);

发出请求时,出现此错误:

IncompleteSignatureException: '/pricing/aws4_request' not a valid key=value pair (missing equal-sign) in Authorization header: 'AWS4-HMAC-SHA256 Credential=AKIAJEYLBU6ZZT32VEGQ/20180830/ap-south-1    /pricing/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-target;x-amz-user-agent, Signature=470f715a40846fe621e88c639d0847a0e5a499b14ec8e1a5dd7a56c1657e87c8'.

,我不知道出了什么问题,因为我正在设置凭据。你能帮我吗?

0 个答案:

没有答案