davidtsadler / ebay-sdk-php

An eBay SDK for PHP. Use the eBay API in your PHP projects.
Apache License 2.0
350 stars 343 forks source link

Support creating OAUTH tokens. #75

Closed davidtsadler closed 7 years ago

davidtsadler commented 7 years ago

I think it will be helpful if the SDK made it easier to generate OAUTH tokens for the API. It will need to support both the Application and User tokens.

$sdk = new DTS\eBaySDK\Sdk([
    'sandbox'     => true,
    'credentials' => [
        'appId'  => '111',
        'certId' => '222',
        'devId'  => '333',
        'ruName' => 'foo'
    ]
]);

$oAuthService = $sdk->createOAuthService();

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

/**
 * Outputs (wrapped for readability)
 *
 * https://signin.sandbox.ebay.com/authorize?
 *   client_id=111&
 *   redirect_uri=foo&
 *   response_type=code&
 *   state=bar&
 *   scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.account%20
 *     https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.inventory
 */

// Exchange the auth code for a token.
$response = $oAuthService->getUserToken('<USER AUTH CODE>');

echo $response->access_token;
echo $response->expires_in;
echo $response->refresh_token;

// Refreshing a user token.
$response = $oAuthService->refreshUserToken([
    'refreshToken' => '<USER REFRESH TOKEN>',
    'scope' => [
      'https://api.ebay.com/oauth/api_scope/sell.account',
      'https://api.ebay.com/oauth/api_scope/sell.inventory'
    ]
]);

echo $response->access_token;
echo $response->expires_in;

// Getting an application token could be as simple as:
$response = $oAuthService->getAppToken();

echo $response->access_token;
echo $response->expires_in;