Magento Rest API错误:`签名无效,请验证并重试,`添加searchCriteria筛选器后

idv4meu8  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(143)

我试图发送一个API请求到magento 2 API检索所有订单。我从Laravel使用Illuminate\Support\Facades\Http发送此请求。为了使用集成Oauth凭据向magento im验证自己。我将集成资源访问权限设置为“all”,以使测试更容易
Magento版本2.4.4

$orderEndpoint = '/rest/V1/orders';

$oauthParams = [
    'oauth_consumer_key' => $clientId,
    'oauth_nonce' => md5(uniqid(rand(), true)),
    'oauth_signature_method' => 'HMAC-SHA256',
    'oauth_timestamp' => time(),
    'oauth_token' => $accessToken,
];

ksort($oauthParams);

$queryParams = [
    'searchCriteria[currentPage]' => 1,
    'searchCriteria[pageSize]' => 10,
];

$baseString = 'GET&' . rawurlencode($baseUrl . $orderEndpoint) . '&' . rawurlencode(http_build_query(array_merge($oauthParams, $queryParams), '', '&', PHP_QUERY_RFC3986));
dump($baseString);

$signatureKey = rawurlencode($consumerSecret) . '&' . rawurlencode($tokenSecret);
$signature = base64_encode(hash_hmac('sha256', $baseString, $signatureKey, true));
dump($signature);

$oauthParams['oauth_signature'] = $signature;

$authorizationHeader = 'OAuth ' . http_build_query($oauthParams, '', ', ', PHP_QUERY_RFC3986);
$fullUrl = $baseUrl . $orderEndpoint . '?' . http_build_query(array_merge($oauthParams, $queryParams));
dump($fullUrl);

$response = Http::withHeaders([
    'Authorization' => $authorizationHeader,
])->get($baseUrl . $orderEndpoint, $queryParams);

if ($response->successful()) {
    $orderData = $response->json();
    dd($orderData);
} else {
    dd($response->json());
}

字符串
这是我的代码,它像它应该工作,但当我试图改变$queryParams:

$queryParams = [
    'searchCriteria[filter_groups][0][filters][0][field]' => 'status',
    'searchCriteria[filter_groups][0][filters][0][value]' => 'complete',
    'searchCriteria[currentPage]' => 1,
    'searchCriteria[pageSize]' => 10,
];


我收到错误消息签名无效。请验证并重试。
有人知道我做错了什么吗?解决办法是什么?

xlpyo6sf

xlpyo6sf1#

该问题的实际解决方法是愚蠢的,过滤器必须按字母顺序排列.我的代码工作,如果我像这样格式化它:

$queryParams = [
    'searchCriteria[currentPage]' => 1,
    'searchCriteria[filter_groups][0][filters][0][field]' => 'status',
    'searchCriteria[filter_groups][0][filters][0][value]' => 'complete',
    'searchCriteria[filter_groups][1][filters][0][condition_type]' => 'gt',
    'searchCriteria[filter_groups][1][filters][0][field]' => 'created_at',
    'searchCriteria[filter_groups][1][filters][0][value]' => Carbon::now()->subWeek()->toTimeString(),
    'searchCriteria[pageSize]' => 10,
];

字符串
它的工作...

相关问题