【问题标题】:Ebay: Auth token is invalid. Validation of the authentication token in API request failedEbay:身份验证令牌无效。 API 请求中的身份验证令牌验证失败
【发布时间】:2018-03-29 20:11:12
【问题描述】:

我正在尝试使用Ebay PHP SDK 连接到 Ebay 并获取销售商品的卖家。为此,我使用了以下步骤:

第 1 步:获取登录用户的授权令牌和代码。我使用以下代码来实现。

use \DTS\eBaySDK\OAuth\Services as OauthService;
use \DTS\eBaySDK\OAuth\Types as OauthType;

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

$service = new OauthService\OAuthService([
    'credentials' => $config['sandbox']['credentials'],
    'ruName'      => $config['sandbox']['ruName'],
    'sandbox'     => true
]);
$oauthParam = [
  'client_id' => $config['sandbox']['credentials']['appId'],
  'redirect_uri' => $config['sandbox']['redirect_uri'],
  'response_type' => 'code',
  'scope' => 'https://api.ebay.com/oauth/api_scope'
];
$urlParam = '';
$query = [];
foreach($oauthParam as $key => $param) {
    $query[] = "$key=$param";
}
$urlParam = '?' . implode('&', $query);
$url = 'https://signin.sandbox.ebay.com/authorize' . $urlParam;
@session_start();

if(isset($_SESSION['ebay_oauth_token'])) {
    $token = $_SESSION['ebay_oauth_token']['code'];
}
else {
    if(isset($_GET['code'])) {
        $token = $_GET['code'];
        $_SESSION['ebay_oauth_token']['code'] = $token;

        $request = new OauthType\GetUserTokenRestRequest();
        $request->code = $token;

        $response = $service->getUserToken($request);

        if ($response->getStatusCode() !== 200) {
            //Error
        } else {
            $_SESSION['ebay_oauth_token']['access_token'] = $response->access_token;
        }
    } else {
        @header('location: ' . $url);
    }

}

$userOauthToken = $_SESSION['ebay_oauth_token']['access_token'];

上面的代码按预期工作。即用户被重定向到登录页面进行授权并获得一组代码和访问令牌。

第 2 步:使用从 Step #1 获得的 code 获取 Selling Items。我使用以下代码来实现该功能。

$request->RequesterCredentials = new Types\CustomSecurityHeaderType();
$request->RequesterCredentials->eBayAuthToken = $token; //Obtained from Step 1

$request->ActiveList = new Types\ItemListCustomizationType();
$request->ActiveList->Include = true;
$request->ActiveList->Pagination = new Types\PaginationType();
$request->ActiveList->Pagination->EntriesPerPage = 10;
$request->ActiveList->Sort = Enums\ItemSortTypeCodeType::C_CURRENT_PRICE_DESCENDING;

$pageNum = 1;

do {
    $request->ActiveList->Pagination->PageNumber = $pageNum;
    $response = $service->getMyeBaySelling($request);
    if (isset($response->Errors)) {
        //Error Output
    }

    if ($response->Ack !== 'Failure' && isset($response->ActiveList)) {
        foreach ($response->ActiveList->ItemArray->Item as $item) {
            //Output response
        }
    }

    $pageNum += 1;

} while ({condition});

我在Step #2 中遇到问题。它在运行代码时生成Invalid Token

如果有人帮助我,我将不胜感激。

【问题讨论】:

    标签: php ebay-api


    【解决方案1】:

    您正在混合请求。在您的代码的第二部分中,请求属于使用 Auth'n'Auth 令牌的交易 API,并且您正在尝试使用 OAuth 令牌进行调用。这 2 个令牌是不同的,适用于不同的 API。

    您有 2 个选项。

    1. 要么保留代码的第二部分,这实际上似乎是正确的,但使用 Auth'n'Auth 令牌(您可以从开发人员帐户生成)。在这种情况下,第一部分是没有用的。

    2. 保留代码的第一部分,并删除第二部分。在这种情况下,您需要使用 OAuth API 而不是 Trading API 重写您的第二部分代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多