public function run()
    {
        // Respond after a successful OAuth2 redirect.
        if (isset($_GET['state'], $_GET['code'])) {
            if ($_GET['state'] !== $this->state) {
                $this->reportError('Invalid state parameter');
                return;
            }
            if (!file_put_contents($this->file, $_GET['code'], LOCK_EX)) {
                $this->reportError('Failed to write authorization code to file');
                return;
            }
            $this->setRedirect($this->localUrl . '/?done');
            $this->response->content = '<p>Authentication response received, please wait...</p>';

            return;
        }

        // Show the final result page.
        if (array_key_exists('done', $_GET)) {
            $this->response->content = '<p><strong>Successfully logged in.</strong></p>'
                . '<p>You can return to the command line.</p>';

            return;
        }

        // Redirect to login.
        $url = $this->getOAuthUrl();
        $this->setRedirect($url);
        $this->response->content = '<p><a href="' . htmlspecialchars($url) .'">Log in</a>.</p>';
        return;
    }