0

我正在使用 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());
4

0 回答 0