90 lines
2.7 KiB
PHP
90 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* Initial authentication script for Zoho OAuth2
|
|
*
|
|
* Step 1: Run this script to get the authorization URL
|
|
* Step 2: Visit the URL and authorize the application
|
|
* Step 3: Copy the authorization code from the redirect URL
|
|
* Step 4: Run: php scripts/authenticate.php --code=YOUR_CODE
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Autoloader
|
|
spl_autoload_register(function ($class) {
|
|
$prefix = 'App\\';
|
|
$baseDir = __DIR__ . '/../src/';
|
|
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) !== 0) {
|
|
return;
|
|
}
|
|
|
|
$relativeClass = substr($class, $len);
|
|
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
|
|
|
|
if (file_exists($file)) {
|
|
require $file;
|
|
}
|
|
});
|
|
|
|
use App\Core\ZohoClient;
|
|
|
|
// Load configuration
|
|
$config = require __DIR__ . '/../config/config.php';
|
|
|
|
// Parse command line options
|
|
$options = getopt('', ['service:', 'code:', 'help']);
|
|
|
|
$service = $options['service'] ?? 'projects';
|
|
$help = isset($options['help']);
|
|
|
|
if ($help) {
|
|
echo "Zoho OAuth2 Authentication Script\n";
|
|
echo "=================================\n\n";
|
|
echo "Usage:\n";
|
|
echo " 1. Generate authorization URL:\n";
|
|
echo " php scripts/authenticate.php --service=projects\n\n";
|
|
echo " 2. Visit the URL, authorize, and copy the code from redirect URL\n\n";
|
|
echo " 3. Exchange code for tokens:\n";
|
|
echo " php scripts/authenticate.php --service=projects --code=YOUR_CODE\n\n";
|
|
echo "Options:\n";
|
|
echo " --service Zoho service (projects, desk)\n";
|
|
echo " --code Authorization code from OAuth flow\n";
|
|
echo " --help Show this help message\n";
|
|
exit(0);
|
|
}
|
|
|
|
$client = new ZohoClient($config);
|
|
|
|
if (isset($options['code'])) {
|
|
// Exchange authorization code for tokens
|
|
$code = $options['code'];
|
|
|
|
echo "Exchanging authorization code for tokens...\n";
|
|
|
|
try {
|
|
$client->exchangeCodeForTokens($code, $service);
|
|
echo "Success! Tokens have been saved.\n";
|
|
echo "You can now run the export script.\n";
|
|
} catch (\Exception $e) {
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
} else {
|
|
// Generate authorization URL
|
|
try {
|
|
$authUrl = $client->getAuthorizationUrl($service);
|
|
echo "Authorization URL for Zoho {$service}:\n";
|
|
echo "========================================\n";
|
|
echo "{$authUrl}\n\n";
|
|
echo "1. Visit this URL in your browser\n";
|
|
echo "2. Authorize the application\n";
|
|
echo "3. Copy the 'code' parameter from the redirect URL\n";
|
|
echo "4. Run: php scripts/authenticate.php --service={$service} --code=YOUR_CODE\n";
|
|
} catch (\Exception $e) {
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
}
|