fwrite(STDERR, "Unrecognized option \"{$arg}\".\n"); fwrite(STDERR, "Run with no arguments for help\n"); } } else { array_push($remainingArgs, $arg); } } if (count($remainingArgs) !== 3) { fwrite(STDERR, "Expecting exactly 3 non-option arguments, got " . count($remainingArgs) . "\n"); fwrite(STDERR, "Run with no arguments for help\n"); die; } $appInfoFile = $remainingArgs[0]; $oauth1AccessToken = new dbx\OAuth1AccessToken($remainingArgs[1], $remainingArgs[2]); try { list($appInfoJson, $appInfo) = dbx\AppInfo::loadFromJsonFileWithRaw($appInfoFile); } catch (dbx\AppInfoLoadException $ex) { fwrite(STDERR, "Error loading <app-info-file>: " . $ex->getMessage() . "\n"); die; } // Get an OAuth 2 access token. $upgrader = new dbx\OAuth1Upgrader($appInfo, "examples-authorize", "en"); $oauth2AccessToken = $upgrader->createOAuth2AccessToken($oauth1AccessToken); echo "OAuth 2 access token obtained.\n"; // Write out auth JSON. $authArr = array("access_token" => $oauth2AccessToken); if (array_key_exists('host', $appInfoJson)) { $authArr['host'] = $appInfoJson['host']; } $json_options = 0; if (defined('JSON_PRETTY_PRINT')) {
throw new \Exception('This program was meant to be run from the command-line and not as a web app. Bad value for PHP_SAPI. Expected \'cli\', given \'' . PHP_SAPI . '\'.', 1); } // Make sure the Dropbox SDK has been installed if (!@is_dir(dirname(__DIR__) . '/vendor/dropbox/dropbox-sdk')) { throw new \Exception('Please install the Dropbox SDK by running \'composer install\' in the edropub root directory.', 2); } // Require common settings require_once dirname(__DIR__) . '/config/common.php'; // Activate error reporting require_once dirname(__DIR__) . '/vendor/dropbox/dropbox-sdk/lib/Dropbox/strict.php'; // Include the Dropbox autoloader in case the composer autoloader is not already active if (!class_exists('\\Dropbox\\AppInfo')) { echo "auto\n\n"; require_once dirname(__DIR__) . '/vendor/dropbox/dropbox-sdk/lib/Dropbox/autoload.php'; } list($appInfoJson, $appInfo) = \Dropbox\AppInfo::loadFromJsonFileWithRaw(dirname(__DIR__) . '/config/config.json'); // This is a command-line tool (as opposed to a web app), so we can't supply a redirect URI. $webAuth = new \Dropbox\WebAuthNoRedirect($appInfo, "examples-authorize", "en"); $authorizeUrl = $webAuth->start(); echo "1. Go to: {$authorizeUrl}\n"; echo "2. Click \"Allow\" (you might have to log in first).\n"; echo "3. Copy the authorization code.\n"; echo "Enter the authorization code here: "; $authCode = \trim(\fgets(STDIN)); list($accessToken, $userId) = $webAuth->finish($authCode); echo "Authorization complete.\n"; echo "- User ID: {$userId}\n"; echo "- Access Token: {$accessToken}\n"; $authArr = array('access_token' => $accessToken, 'editorially_prefix' => '/Apps/Editorially', 'leadpub_api_key' => '<YOUR_ACCOUNT_API_KEY>', 'leadpub_prefix' => '/<YOUR_BOOKS_SLUG>', 'leanpub_trigger' => 'preview'); if (array_key_exists('host', $appInfoJson)) { $authArr['host'] = $appInfoJson['host'];