【问题标题】:How to generate eBay OAuth user token?如何生成 eBay OAuth 用户令牌?
【发布时间】:2019-06-30 20:20:24
【问题描述】:

我一直在使用来自 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)。

【问题讨论】:

    标签: php ebay-api ebay-sdk


    【解决方案1】:

    您可以使用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");
    

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

    $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-&gt;access_token 变量中。该令牌是短暂的,因此如果您想使用它,您需要不时更新它。为此,请使用$response-&gt;refresh_token 并致电$service-&gt;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
    

    【讨论】:

    • $token = $_GET["code"];在您的示例中未使用
    • @s_h 更新了我的答案。这基本上就是您的问题。
    猜你喜欢
    • 2017-11-20
    • 2017-10-18
    • 1970-01-01
    • 2015-09-18
    • 2019-03-13
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2013-01-23
    相关资源
    最近更新 更多