【问题标题】:What changes to make to shopping_get_item.php and ShoppingBaseService.php in ebay-sdk-php to make it work with X-EBAY-API-IAF-TOKEN对 ebay-sdk-php 中的 shopping_get_item.php 和 ShoppingBaseService.php 进行哪些更改以使其与 X-EBAY-API-IAF-TOKEN 一起使用
【发布时间】:2021-10-19 09:29:58
【问题描述】:

我正在使用来自davidtsadler/ebay-sdk-examples reposity02-get-single-item.php 代码示例。 这是完整的代码示例:

<?php
/**
 * Copyright 2016 David T. Sadler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Include the SDK by using the autoloader from Composer.
 */
require __DIR__.'/../vendor/autoload.php';

/**
 * Include the configuration values.
 *
 * Ensure that you have edited the configuration.php file
 * to include your application keys.
 */
$config = require __DIR__.'/../configuration.php';

/**
 * The namespaces provided by the SDK.
 */
use \DTS\eBaySDK\Shopping\Services;
use \DTS\eBaySDK\Shopping\Types;
use \DTS\eBaySDK\Shopping\Enums;

/**
 * Create the service object.
 */
$service = new Services\ShoppingService([
    'credentials' => $config['production']['credentials']
]);

/**
 * Create the request object.
 */
$request = new Types\GetSingleItemRequestType();

/**
 * Specify the item ID of the listing.
 */
$request->ItemID = '111111111111';

/**
 * Specify that additional fields need to be returned in the response.
 */
$request->IncludeSelector = 'ItemSpecifics,Variations,Compatibility,Details';

/**
 * Send the request.
 */
$response = $service->getSingleItem($request);

/**
 * Output the result of calling the service operation.
 */
if (isset($response->Errors)) {
    foreach ($response->Errors as $error) {
        printf(
            "%s: %s\n%s\n\n",
            $error->SeverityCode === Enums\SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
            $error->ShortMessage,
            $error->LongMessage
        );
    }
}

if ($response->Ack !== 'Failure') {
    $item = $response->Item;

    print("$item->Title\n");

    printf(
        "Quantity sold %s, quantiy available %s\n",
        $item->QuantitySold,
        $item->Quantity - $item->QuantitySold
    );

    if (isset($item->ItemSpecifics)) {
        print("\nThis item has the following item specifics:\n\n");

        foreach ($item->ItemSpecifics->NameValueList as $nameValues) {
            printf(
                "%s: %s\n",
                $nameValues->Name,
                implode(', ', iterator_to_array($nameValues->Value))
            );
        }
    }

    if (isset($item->Variations)) {
        print("\nThis item has the following variations:\n");

        foreach ($item->Variations->Variation as $variation) {
            printf(
                "\nSKU: %s\nStart Price: %s\n",
                $variation->SKU,
                $variation->StartPrice->value
            );

            printf(
                "Quantity sold %s, quantiy available %s\n",
                $variation->SellingStatus->QuantitySold,
                $variation->Quantity - $variation->SellingStatus->QuantitySold
            );

            foreach ($variation->VariationSpecifics as $specific) {
                foreach ($specific->NameValueList as $nameValues) {
                    printf(
                        "%s: %s\n",
                        $nameValues->Name,
                        implode(', ', iterator_to_array($nameValues->Value))
                    );
                }
            }
        }
    }

    if (isset($item->ItemCompatibilityCount)) {
        printf("\nThis item is compatible with %s vehicles:\n\n", $item->ItemCompatibilityCount);

        // Only show the first 3.
        $limit = min($item->ItemCompatibilityCount, 3);
        for ($x = 0; $x < $limit; $x++) {
            $compatibility = $item->ItemCompatibilityList->Compatibility[$x];
            foreach ($compatibility->NameValueList as $nameValues) {
                printf(
                    "%s: %s\n",
                    $nameValues->Name,
                    implode(', ', iterator_to_array($nameValues->Value))
                );
            }
            printf("Notes: %s \n", $compatibility->CompatibilityNotes);
        }
    }
}

它以前工作正常。但是 eBay 对他们的 API 购物请求进行了一些更改。自 2021 年 7 月 1 日起,在请求标头中还应传递 X-EBAY-API-IAF-TOKEN。他们在GetSingleItem 页面上通知。

所以现在,我从 eBay API 收到了这个响应:

RepeatableType {#4019 ▼
  -data: array:1 [▼
    0 => ErrorType {#4017 ▼
      -values: array:5 [▼
        "ShortMessage" => "Token not available in request."
        "LongMessage" => "Token not available in request. Please specify a valid token as HTTP header."
        "ErrorCode" => "1.33"
        "SeverityCode" => "Error"
        "ErrorClassification" => "RequestError"
      ]
      -attachment: array:2 [▼
        "data" => null
        "mimeType" => null
      ]
    }
  ]
  -position: 0
  -class: "DTS\eBaySDK\Shopping\Types\GetSingleItemResponseType"
  -property: "Errors"
  -expectedType: "DTS\eBaySDK\Shopping\Types\ErrorType"
}

我正在使用上面的代码,还使用SDK library 使其工作。

我看到有些人正在更改他们的 Shopping/Services/ShoppingBaseService.php,他们将 X-EBAY-API-IAF-TOKEN 添加到代码中:

我尝试了很多不同的方法来解决这个问题,但仍然没有运气。

所以,我的问题是我需要对 02-get-single-item.phpShopping/Services/ShoppingBaseService.php 进行哪些更改,以及我需要在哪些地方进行更改才能使 GetSingleItem 请求在标头中包含 X-EBAY-API-IAF-TOKEN并得到正确的回应?

提前非常感谢!

【问题讨论】:

    标签: php ebay-api ebay-sdk


    【解决方案1】:

    其实我已经找到方法了。 这些是我对Shopping/Services/ShoppingBaseService.php所做的更改

    这是我对02-get-single-item.php 文件所做的更改,以便在 API 请求中使用 oauthUserToken:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多