AWS-Guzzle我们计算出的请求签名与您提供的签名不匹配。检查您的AWS Secret Access密钥和签名方法

时间:2018-08-13 09:23:40

标签: amazon-web-services aws-api-gateway guzzle guzzlehttp psr-7

我正在使用SignatureV4 AWS身份验证来验证我的API调用, 我也在使用Guzzle / Client来调用API。

通过执行上述操作,我的API调用仅适用于GET方法,而不适用于POST方法。对于POST方法调用,它显示一个错误(我们计算出的请求签名与您提供的签名不匹配。请检查您的AWS秘密访问密钥和签名方法)

我的代码:

use Aws\Credentials\CredentialProvider;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$request = new Request(
    'POST',
    'https://XXXXXXXXXXXXx.com/XXXXXx/XXXXXXx',
    [
        'body' => '{
            "param1": "loream",
            "param2": "4970",
            "param3": "1",
            "param4": "2.0",
            "param5": "mL",
            "param6": "kgs",
            "param7": 0,
        }'
    ]      
);

$key['credentials'] = array( 
    'key'    => 'XXXXXXXXXXXXXXX-access-key',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-secret-key'
);

$credentials = call_user_func(CredentialProvider::defaultProvider(
    $key
));

// Construct a request signer
$signer = new SignatureV4('execute-api', 
                        'us-east-1'
);

// Sign the request
$request = $signer->signRequest($request, $credentials);

// Send the request
try {
    $response = (new Client)->send($request);
    print_r($response);
}
    catch (Exception $exception) {
    $responseBody = $exception->getResponse()->getBody(true);
    echo $responseBody;
}
$response = (new Client)->send($request);

$results = json_decode($response->getBody());

0 个答案:

没有答案