3

我一直在使用 David Sadler 的这个ebay-sdk-php来生成对 Ebay API 的交易调用,但首先我必须创建 OAuthUserToken。

我使用了 gettoken.php 示例并创建了以下代码:

    $service = new \DTS\eBaySDK\OAuth\Services\OAuthService([
        'credentials'   => config('ebay.'.config('ebay.mode').'.credentials'),
        'ruName' => config('ebay.'.config('ebay.mode').'.ruName'),
        'sandbox'     => true
    ]);

    $token = session('????'); //here I have to retrieve the authorization callback information.
    /**
     * Create the request object.
     */
    $request = new \DTS\eBaySDK\OAuth\Types\GetUserTokenRestRequest();
    $request->code = $token;
    /**
     * Send the request.
     */
    $response = $service->getUserToken($request);

由于某种原因,我无法为 UserOauth 令牌生成重定向。我想那个代码:

$service = new\DTS\eBaySDK\OAuth\Services\OAuthService([

...自动生成到 eBay Grant Area 的重定向,但事实并非如此。

有谁知道如何解决这个问题?我想知道如何授予用户访问权限,然后执行呼叫(例如getEbayTime)。

4

1 回答 1

2

您可以使用redirectUrlForUser()函数生成用于重定向的 URL。

$url =  $service->redirectUrlForUser([
    'state' => '<state>',
    'scope' => [
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.inventory'
    ]
]);

然后,调用例如header()重定向用户。请注意,您不能在标头调用之前显示任何文本/html。

header("Location: $url");

$_GET["code"]之后,当用户从 ebay 网站返回时,您的令牌应该存储在其中。

$token = $_GET["code"];

因此,您可以发送请求并使用示例获取 OAuth 令牌。

$request = new \DTS\eBaySDK\OAuth\Types\GetUserTokenRestRequest();
$request->code = $token;

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

// Output the result of calling the service operation.
printf("\nStatus Code: %s\n\n", $response->getStatusCode());
if ($response->getStatusCode() !== 200) {
    // Display information that an error has happened
    printf(
        "%s: %s\n\n",
        $response->error,
        $response->error_description
    );
} else {
    // Use the token to make calls to ebay services or store it.
    printf(
        "%s\n%s\n%s\n%s\n\n",
        $response->access_token,
        $response->token_type,
        $response->expires_in,
        $response->refresh_token
    );
}

您的 OAuth 令牌将在$response->access_token变量中。该令牌是短暂的,因此如果您想使用它,您需要不时更新它。为此,请使用$response->refresh_token并调用$service->refreshUserToken().

$response = $service->refreshUserToken(new Types\RefreshUserTokenRestRequest([
    'refresh_token' => '<REFRESH TOKEN>',
    'scope' => [
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.inventory'
    ]
]));
// Handle it the same way as above, when you got the first OAuth token
于 2019-07-08T20:49:08.793 回答