|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once 'path/to/vendor/autoload.php'; |
| 4 | + |
| 5 | +// Initialize our storage engine. Replace this with whichever storage mechanism you want to use |
| 6 | +$storageEngine = new StorageEngine(); |
| 7 | +// Set our arguments for the SDK. These should be stored in another file |
| 8 | +$options = [ |
| 9 | + 'api_key' => '<shopify_api_key>', |
| 10 | + 'api_secret' => '<shopify_api_secret>', |
| 11 | + 'redirect_url' => 'https://localhost/shopify', |
| 12 | + 'store' => '<store_name>', |
| 13 | + 'permissions' => 'read_products', |
| 14 | +]; |
| 15 | + |
| 16 | +// Initialize the SDK using arguments |
| 17 | +\Shopify\Shopify::init($options); |
| 18 | + |
| 19 | +/* Now we check if we are starting an OAuth request request, or handling a |
| 20 | + response from Shopify. Checking for ?code=<auth_code> should suffice */ |
| 21 | +if( !isset( $_GET['code'] ) ) |
| 22 | +{ |
| 23 | + // We need to initiate the OAuth flow. Generate a nonce we can store for |
| 24 | + // security purposes |
| 25 | + $nonce = \Shopify\Auth::generateNonce(); |
| 26 | + |
| 27 | + // In order to compare the nonce when they return, it needs to be stored somewhere. |
| 28 | + $storageEngine->store('auth_nonce', $nonce); |
| 29 | + |
| 30 | + // We can now send the user to Shopify for authentication |
| 31 | + header("Location: ".\Shopify\Auth::authorizationUrl()); |
| 32 | +} |
| 33 | +else |
| 34 | +{ |
| 35 | + // We are handling an authentication resposne from Shopify. We need to validate the |
| 36 | + // request, and then exchange the code for an access token |
| 37 | + |
| 38 | + // First, we get the nonce we stored |
| 39 | + $nonce = $storageEngine->retrieve('auth_nonce'); |
| 40 | + // Set the nonce property of \Shopify\Auth so it can compare response -> request |
| 41 | + \Shopify\Auth::setNonce($nonce); |
| 42 | + // Finally, attempt to get the access token. If there is no nonce present, or the |
| 43 | + // store nonce does not match the one present in Shopify's response, or the HMAC |
| 44 | + // signature fails, this will throw an exception |
| 45 | + try { |
| 46 | + $accessToken = \Shopify\Auth::accessToken(); |
| 47 | + } catch(Exception $e) { |
| 48 | + echo $e->getMessage(); // Authentication failed for some reason |
| 49 | + exit(); |
| 50 | + } |
| 51 | + |
| 52 | + echo $accessToken; // 53e20e750c89274d02b53927135fd664 |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +/** |
| 57 | + * This is a very crude representation of session management. |
| 58 | + * This could be replaced with DB storage, redis, or your frameworks session library |
| 59 | + * |
| 60 | + * **For demo purposes only** |
| 61 | + */ |
| 62 | +class StorageEngine { |
| 63 | + public function __construct() { |
| 64 | + if (session_status() == PHP_SESSION_NONE) { |
| 65 | + session_start(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function store($key, $value) { |
| 70 | + $_SESSION[$key] = $value; |
| 71 | + } |
| 72 | + |
| 73 | + public function retrieve($key) { |
| 74 | + return $_SESSION[$key]; |
| 75 | + } |
| 76 | +} |
0 commit comments