Walmart.io身份验证问题-无法对请求中的身份验证签名进行身份验证

时间:2020-07-10 16:03:19

标签: php openssl phpseclib walmart-api

我正在尝试与Walmart.io API链接以从其资源中获取一些数据。但是我被困在第一阶段。

根据Walmart.io快速入门文档(https://walmart.io/docs/affiliate/quick-start-guide),我应该遵循以下步骤:

  1. 使用Walmart.io创建帐户
  2. 为Web应用程序创建应用程序
  3. 生成证书(根据他们的指南,应该有一些自动生成证书的功能,但我没有找到它)
  4. 将公钥上传到应用程序
  5. 我们将获取消费者ID和密钥版本,并可以使用该版本和私钥进行请求。我们还需要添加其他包含标头和时间戳的标头。

所以,我做了所有事情,但仍然无法正常工作。

我正在使用Open SSL生成他们建议的私钥和公钥:https://walmart.io/key-tutorial 我尝试避免使用-des3,这样它也不会要求我输入密码,但这也不起作用。

这是我尝试过的脚本

curl --location --request GET 'https://developer.api.walmart.com/api-proxy/service/affil/product/v2/taxonomy' \
--header 'WM_SEC.KEY_VERSION: 2' \
--header 'WM_CONSUMER.ID: <Consumer_ID>' \
--header 'WM_CONSUMER.INTIMESTAMP: 1594389945813' \
--header 'WM_SEC.AUTH_SIGNATURE: W5PEHIew3LsnATk0zxJddeo416YEpMIjvk1b7lW9VMIZFx55erc/5df/FK9UtS5i48q057oASo0AX3SDd2hx+QSeyiX3FtLAgAgiZnGqQ6nJndySWgL5ih/GaUTXIC6dd048GFEZlC6axXdGoTWNzX9P0n/2DwLF9EtvMjdvjB1kum0z2xKz/lQGlvnjVkGK9sZdSUa5rfgxKSPi7ix+LRIJWYwt6mTKUlGz2vP1YjGcZ7gVwAs9o8iFC//0rHUWFwaEGrT0aZJtS7fvSFtKj5NRfemX4fwRO4cgBRxPWy9MRooQwXPmKxRP75PxHKTerv8X6HvRo0GdGut+2Krqxg==' \

我得到的答复是

{
    "details": {
        "Description": "Could not authenticate in-request, auth signature :  Signature verification failed: affil-product, version: 2.0.0, env: prod",
        "wm_svc.version": "2.0.0",
        "wm_svc.name": "affil-product",
        "wm_svc.env": "prod"
    }
}

希望有人给我一些有关这个问题的见识。

预先感谢

3 个答案:

答案 0 :(得分:0)

我以前遇到过此问题,您似乎要签名的数据格式似乎不正确。

在节点中,模板字符串的内容应如下所示:${consumerId}\n${timeStamp}\n${keyVersion}\n

答案 1 :(得分:0)

以下是基于 Abiral 的上述帖子的完整示例:

<?php

/**
 * Sample script to sign and send a request to the Walmart Affiliate Marketing API.
 *
 * https://walmart.io/docs/affiliate/introduction
 *
 * Usage:
 *   1. Fill out the required variables at the top of this script.
 *   2. Install dependencies via composer install.
 *   3. Run via php index.php or by opening this script in a browser.
 *
 * Acknowledgements:
 *   Abiral Neupane at https://stackoverflow.com/a/62847241/1120652
 *   @gorenstein at https://gitter.im/IO-support/community?at=5f2e5d2051bb7d3380d9b58b
 */

include './vendor/autoload.php';

use \GuzzleHttp\Client;

/**
 * Create an account at Walmart.io. Then create an application. Then follow the
 * steps at https://walmart.io/key-tutorial to create a set of keys. Upload
 * the public key (its contents start with BEGIN PUBLIC KEY) into the
 * production environment of the application that you created.
 */
$consumer_id = 'Paste here the consumer id that you will see in your application details after pasting the public key';
$key = 'Paste here the private key. Full, including BEGIN and END PRIVATE KEY lines.';

$version = '1';
$timestamp = round(microtime(true) * 1000);
$message = $consumer_id . "\n" . $timestamp . "\n" . $version . "\n";

$pkeyid = openssl_pkey_get_private($key);
openssl_sign($message, $signature, $pkeyid, OPENSSL_ALGO_SHA256);
$signature = base64_encode($signature);
openssl_free_key($pkeyid);

$api = 'https://developer.api.walmart.com';
$product_resource = 'api-proxy/service/affil/product/v2/items/316226539';
$client = new Client(['base_uri' => $api]);
$response = $client->get($product_resource, [
  'headers' => [
    'WM_SEC.KEY_VERSION' => $version,
    'WM_CONSUMER.ID' => $consumer_id,
    'WM_CONSUMER.INTIMESTAMP' => $timestamp,
    'WM_SEC.AUTH_SIGNATURE' => $signature,
  ]
]);

print_r(json_decode($response->getBody()->__toString()));

我在 https://github.com/juampynr/walmart-api-v2-php

发布了上述内容

答案 2 :(得分:-1)

原来是生成的签名有问题(这说明了为什么我更改脚本后它仍然起作用。

这是运行良好的脚本:

make topopt PETSC_DIR=/Users/hornymoose/petsc-3.13.3

注意:它将 guzzlehttp / guzzle 库用于HTTP请求