php - 如何生成 eBay OAuth 用户 token ?

标签 php ebay-api ebay-sdk

我一直在用这个ebay-sdk-php从 David Sadler 生成对 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 token 生成重定向。我认为代码:

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

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

有人知道怎么解决吗?我想知道如何授予用户访问权限,然后执行调用(例如 getEbayTime)。

最佳答案

您可以使用 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() 重定向用户。请注意,您不能在 header 调用之前显示任何文本/html。

header("Location: $url");

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

$token = $_GET["code"];

因此,您可以发送请求并使用该示例取回 OAuth token 。

$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 token 将位于 $response->access_token 变量中。 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

关于php - 如何生成 eBay OAuth 用户 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827786/

相关文章:

php - 用php编辑另一个页面的内容

php - 使用格式显示来自查询的多个值

java - 如何使用现有 eBay token 获取 eBay token 到期日期

php - eBay API - 如何获取大件商品图片?

php - 将 Rails 引入 PHP 商店?或者建立我们已经使用的东西?

PHP 表单不将信息保存到 SQL 数据库

python - 返回 Ajax 的多个项目

javascript - 从 eBay 中的查找商品高级 API 返回商品属性

ebay-api - 由于缺少项目特定类型,无法在 eBay API 上添加项目

python - 如何在 Python 中使用 ebay sdk?