From 76376b3e804829967a3385f8960ae94bd0de8928 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Thu, 29 Jun 2017 10:34:10 +0100 Subject: [PATCH] update: add documentation regarding slim application --- oauth-tokens/README.md | 24 +++++++- oauth-tokens/Server.php | 120 --------------------------------------- oauth-tokens/gen-pem.php | 43 -------------- oauth-tokens/index.php | 22 +------ oauth-tokens/no-ssl.php | 25 -------- 5 files changed, 25 insertions(+), 209 deletions(-) delete mode 100644 oauth-tokens/Server.php delete mode 100644 oauth-tokens/gen-pem.php delete mode 100644 oauth-tokens/no-ssl.php diff --git a/oauth-tokens/README.md b/oauth-tokens/README.md index d7b16c4..4329d4a 100644 --- a/oauth-tokens/README.md +++ b/oauth-tokens/README.md @@ -1,6 +1,6 @@ # OAuth Examples -These examples show how to use the eBay SDK for PHP to generate OAuth tokens. +These examples show how to use the eBay SDK for PHP to generate OAuth tokens. 1. [Get app token](https://github.com/davidtsadler/ebay-sdk-examples/blob/master/oauth-tokens/01-get-app-token.php) @@ -13,3 +13,25 @@ These examples show how to use the eBay SDK for PHP to generate OAuth tokens. 1. [Refresh user token](https://github.com/davidtsadler/ebay-sdk-examples/blob/master/oauth-tokens/03-refresh-user-token.php) An example that generates an user token via a refresh token. + +### Example Application + +A [basic application](https://github.com/davidtsadler/ebay-sdk-examples/blob/master/oauth-tokens/slim-app.php) built with the Slim framework is available. It allows you to generate both application and user oauth tokens for the sandbox environment. + +Before running the example you will need to configure your eBay developer account so that it can accept oauth tokens for the sandbox environment. This can be done by following the [eBay documentation](http://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-gen-user-token.html#Getting4). When configuring the RuName you must use the following values for the URLs. + +Privacy Policy URL https://127.0.0.1:8080/privacy +Auth Accepted URL https://127.0.0.1:8080/auth-accepted +Auth Declined URL https://127.0.0.1:8080/auth-declined + +The SDK will need the value of the sandbox RuName. This value can be entered into the configuration.php file if you are using one. There is an [example configuration](https://github.com/davidtsadler/ebay-sdk-examples/blob/master/configuration.php.example) file if you need to create one. + +It is possible to use PHP's built in server to run the application. Run the below command from the root directory of the example repository. + +``` +php -S 127.0.0.1:8080 -t oauth-tokens/ +``` + +Open your browser to http://127.0.0.1:8080 and use the various links to generate your oauth tokens. + +One import note is that eBay require you to use HTTPS endpoints. When you generate a user token you will be re-directed to the HTTPS end point on your machine. Your browser will display an error as PHP's server can not serve content over HTTPS. To resolve this simply change the URL in the address bar to HTTP and navigate to a URL that PHP can serve. diff --git a/oauth-tokens/Server.php b/oauth-tokens/Server.php deleted file mode 100644 index 84469e6..0000000 --- a/oauth-tokens/Server.php +++ /dev/null @@ -1,120 +0,0 @@ -server = stream_socket_server( - "ssl://{$host}:{$port}", - $errno, - $errstr, - STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, - $context - ); - - if (!$this->server) { - throw new Exception("$errstr ($errno)"); - } - - $this->host = $host; - $this->port = $port; - } - - public function listen($callback) - { - if (!is_callable($callback)) { - throw new InvalidArgumentException('Callback passed to listen should be callable.'); - } - - printf( - "\nListening on %s:%s\n\n", - $this->host, - $this->port - ); - - printf( - "\nPoint your browser to https://%s:%s\n\n", - $this->host, - $this->port - ); - - while (true) { - $request = ''; - $client = stream_socket_accept($this->server); - if ($client) { - while ($client && !preg_match('/\r?\n\r?\n/', $request)) { - $request .= fread($client, 2046); - if (strlen($request) == 0) { - break; - } - } - fwrite($client, $callback($this->buildSlimEnvironment($request))); - fclose($client); - } - } - } - - private function buildSlimEnvironment($request) - { - return \Slim\Http\Environment::mock( - array_merge( - $this->parseRequest($request), - [ - 'SERVER_PORT' => $this->port - ] - ) - ); - } - - private function parseRequest($request) - { - if (!$request) { - return [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => '/' - ]; - } - $lines = explode("\r\n", $request); - list($method, $uri) = explode(' ', array_shift($lines)); - - $data = [ - 'REQUEST_METHOD' => $method, - 'REQUEST_URI' => $uri - ]; - - foreach ($lines as $line) { - $line = trim($line); - if (strpos($line, ': ') !== false) { - list($key, $value) = explode(': ', $line); - $data['HTTP_'.strtoupper(str_replace('-', '_', $key))] = $value; - } - } - - return $data; - } -} diff --git a/oauth-tokens/gen-pem.php b/oauth-tokens/gen-pem.php deleted file mode 100644 index 03eaeee..0000000 --- a/oauth-tokens/gen-pem.php +++ /dev/null @@ -1,43 +0,0 @@ - 'US', - 'stateOrProvinceName' => $uniq, - 'localityName' => $uniq, - 'organizationName' => "$uniq.com", - 'organizationalUnitName' => $uniq, - 'commonName' => $uniq, - 'emailAddress' => "$uniq@example.com" - ]; - $privateKey = openssl_pkey_new(); - $certificate = openssl_csr_new($certificateData, $privateKey); - $certificate = openssl_csr_sign($certificate, null, $privateKey, 365); - $pem_passphrase = 'abracadabra'; - $pem = []; - openssl_x509_export($certificate, $pem[0]); - openssl_pkey_export($privateKey, $pem[1], $pem_passphrase); - $pem = implode($pem); - $pemfile = __DIR__.'/server.pem'; - file_put_contents($pemfile, $pem); -} diff --git a/oauth-tokens/index.php b/oauth-tokens/index.php index 2403ed0..cbdad64 100644 --- a/oauth-tokens/index.php +++ b/oauth-tokens/index.php @@ -19,25 +19,7 @@ * Include the SDK by using the autoloader from Composer. */ require __DIR__.'/../vendor/autoload.php'; - -/** - * Include a very basic web server. - */ -require __DIR__.'/Server.php'; require __DIR__.'/slim-app.php'; -require __DIR__.'/gen-pem.php'; - -$options = getopt('h:p:'); - -$host = isset($options['h']) ? $options['h'] : '127.0.0.1'; -$port = isset($options['p']) ? $options['p'] : 8080; - -genSelfSignedCertificate(); - -$server = new Server($host, $port); -$server->listen(function ($environment) { - $app = buildSlimApp(); - $app->getContainer()['environment'] = $environment; - return $app->run(true); -}); +$app = buildSlimApp(); +$app->run(); diff --git a/oauth-tokens/no-ssl.php b/oauth-tokens/no-ssl.php deleted file mode 100644 index cbdad64..0000000 --- a/oauth-tokens/no-ssl.php +++ /dev/null @@ -1,25 +0,0 @@ -run();