s_h
s_h

Reputation: 1496

How to generate eBay OAuth user token?

I have been using this ebay-sdk-php from David Sadler to generate a Trading Call to Ebay API, but first I have to create the OAuthUserToken.

I used the gettoken.php example and created the following code:

    $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);

For some reason I cannot generate a redirect for a UserOauth Token. I supposed that code:

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

...automatically generated a redirect to eBay Grant Area, but it was not the case.

Does anyone know how to solve this? I would like to know how to grant the user access and then perform a call (e.g. getEbayTime).

Upvotes: 3

Views: 1443

Answers (1)

Genhis
Genhis

Reputation: 1524

You can use redirectUrlForUser() function to generate the URL for redirection.

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

Then, call e.g. header() to redirect the user. Note that you can't display any text/html before the header call.

header("Location: $url");

After that, your token should be stored in $_GET["code"] when the user returns from ebay website.

$token = $_GET["code"];

So, you can send your request and use the example to get the OAuth token back.

$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
    );
}

Your OAuth token will be in $response->access_token variable. The token is short-lived, so you need to renew it from time to time if you want to use it. To do that, use $response->refresh_token and call $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

Upvotes: 2

Related Questions