<?php include 'autoload.php'; use Shopify\Api\Client; use Shopify\Api\AuthenticationGateway; use Shopify\HttpClient\CurlHttpClient; use Shopify\Redirector\HeaderRedirector; $settings = json_decode(file_get_contents('settings.json')); if (!empty($settings->shopName) && !empty($_GET['code'])) { Client::doValidateSignature($settings->clientSecret, $_GET) or die('Signature validation failed'); $auth = new AuthenticationGateway(new CurlHttpClient(null, false, false), new HeaderRedirector()); $token = $auth->forShopName($settings->shopName)->usingClientId($settings->clientId)->usingClientSecret($settings->clientSecret)->toExchange($_GET['code']); if ($token) { $settings->accessToken = $token; file_put_contents('settings.json', json_encode($settings, JSON_PRETTY_PRINT)); HeaderRedirector::go($settings->redirectUri); } else { die('toExchange failed'); } } if (empty($settings->accessToken)) { die('not authenticated, use install.php first'); } $client = new Client(new CurlHttpClient(null, false, false), $settings); $result = $client->get('/admin/pages.json'); var_dump($result);
public function testExchangingToken() { $shopName = 'shop-name'; $clientId = 'XXX1234567890'; $clientSecret = 'ABC123XYZ'; $temporaryToken = 'TEMP_TOKEN'; $permanentAccessToken = 'ACCESS_TOKEN'; $accessUri = "https://{$shopName}.myshopify.com/admin/oauth/access_token"; $params = array('client_id' => $clientId, 'client_secret' => $clientSecret, 'code' => $temporaryToken); $response = '{"access_token": "' . $permanentAccessToken . '"}'; $this->httpClient->expects($this->once())->method('post')->with($accessUri, $params)->will($this->returnValue($response)); $token = $this->authenticate->forShopName($shopName)->usingClientId($clientId)->usingClientSecret($clientSecret)->toExchange($temporaryToken); $this->assertEquals($accessUri, $this->authenticate->getAccessUri()); $this->assertEquals($permanentAccessToken, $token); }
<?php if (empty($_GET['shopName'])) { die('shopName not provided'); } include 'autoload.php'; use Shopify\Api\AuthenticationGateway; use Shopify\HttpClient\CurlHttpClient; use Shopify\Redirector\HeaderRedirector; if (!file_exists('settings.json')) { copy('settings.json.dist', 'settings.json'); } $settings = json_decode(file_get_contents('settings.json')); $settings->shopName = $_GET['shopName']; $settings->redirectUri = "http://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['REQUEST_URI']) . '/'; file_put_contents('settings.json', json_encode($settings, JSON_PRETTY_PRINT)); $auth = new AuthenticationGateway(new CurlHttpClient(null, false, false), new HeaderRedirector()); $auth->forShopName($settings->shopName)->usingClientId($settings->clientId)->withScope($settings->permissions)->andReturningTo($settings->redirectUri)->initiateLogin();