Javascript 通过pactjs发送时不接受aww4凭据

Javascript 通过pactjs发送时不接受aww4凭据,javascript,typescript,pact,Javascript,Typescript,Pact,我正在尝试验证消费者生成的pact.json。但是,为了验证,我需要包括AWS4凭据,以便能够从我的提供商处获得响应。我正在尝试使用customProviderHeaders执行此操作。我正在使用库AWS4()生成令牌。下面是我的代码: const aws4 = require('aws4'); const path = require('path'); import { before, beforeEach, describe, it } from 'mocha'; const {

我正在尝试验证消费者生成的pact.json。但是,为了验证,我需要包括AWS4凭据,以便能够从我的提供商处获得响应。我正在尝试使用customProviderHeaders执行此操作。我正在使用库AWS4()生成令牌。下面是我的代码:

const aws4 = require('aws4');
const path = require('path');
import { before, beforeEach, describe, it } from 'mocha';

const {
    Verifier
} = require('../../../node_modules/@pact-foundation/pact');


function getToken() {
    const opts: any = {
        method: 'GET',
        region: 'us-east-2',
        service: 'execute-api',
        path: '/qa/api/',
        host: '123456789.execute-api.us-east-2.amazonaws.com',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    };

    aws4.sign(opts, {accessKeyId: '$AWSACCESSKEY', secretAccessKey: '$AWSSECRETKEY'});
    return opts.headers;
}


describe('Pact Verification', () => {

    it('should validate the watchlist expectations', () => {
        let headers = getToken();
        let  authToken = headers.Authorization;
        let date = headers[`X-Amz-Date`];


        let opts = {
            provider: 'DealerBlock',
            providerBaseUrl: 'https://3ua1cprd53.execute-api.us-east-2.amazonaws.com',
            pactUrls: [path.resolve(process.cwd(), 'src/test/pact/path_to_my_json')],
            customProviderHeaders: [`Authorization: ${authToken}`, `X-Amz-Date: ${date}`]
        };

        return new Verifier().verifyProvider(opts)
            .then(output => {
                console.log('STARTED');
                console.log(opts.pactUrls);
                console.log('Pact Verification Complete');
                console.log(output);
            });
    });

});
函数getToken()生成一个新的令牌,然后我获取令牌和日期,并使用客户提供程序头将它们插入到请求中

我看到以下情况:

 INFO: Replacing header 'Authorization: ' with 'Authorization: AWS4-HMAC-SHA256 Credential=AKIAJ5FTCODVMSUTEST/2018908/us-east-2/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=ceea9aac0303769da58357cb37cb849cb0bbfc13ff0a25cea977385368531349'
 INFO: Replacing header 'X-Amz-Date: ' with 'X-Amz-Date: 20180528T184202Z'
但是,我得到以下错误:

Actual: {"message":"The request signature we calculated does not match the signature you provided. 
Check your AWS Secret Access Key and signing method. Consult the service documentation for details."}
我是否以正确的方式使用CustomProviderHeader?或者有人对我应该采取什么不同的做法有什么建议吗?我可以通过邮递员使用相同的凭证发送请求,因此不确定这里发生了什么


谢谢

在我看来还行

可能是因为您没有在以下语句中插入变量(似乎也没有在任何地方定义):

aws4.sign(opts, {accessKeyId: '$AWSACCESSKEY', secretAccessKey: '$AWSSECRETKEY'});

当我通过customProviderHeaders传入“Content Type”:“application/x-www-form-urlencoded”的标题时,我能够实现这一点


尽管此标题列在我的消费者生成的json合同中,但pact提供商似乎没有看到它。

您根据
ops
的内容签署请求,因此最有可能的问题是在签署消息后值发生变化。例如,如果
X-Amz-Date
是签名的一部分,则在创建签名后更改它会导致问题。