$scopes, 'client_id' => $client_id, 'response_type' => 'code', 'access_type' => 'offline', 'redirect_uri' => $redirect_uri, 'prompt' => 'consent' ]); echo "Step 1: Authorization\n"; echo "--------------------\n"; echo "Please open this URL in your browser:\n\n"; echo $auth_url . "\n\n"; echo "After authorization, you'll be redirected to:\n"; echo $redirect_uri . "?code=AUTH_CODE\n\n"; echo "Enter the authorization code from the URL: "; $auth_code = trim(fgets(STDIN)); // Step 2: Exchange code for tokens echo "\nStep 2: Exchanging code for tokens...\n"; echo "-----------------------------------\n"; $token_url = 'https://accounts.zoho.com/oauth/v2/token'; $token_params = [ 'grant_type' => 'authorization_code', 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $auth_code ]; $ch = curl_init($token_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code !== 200) { echo "Error: Failed to get tokens (HTTP $http_code)\n"; echo "Response: " . $response . "\n"; exit(1); } $token_data = json_decode($response, true); if (!isset($token_data['access_token']) || !isset($token_data['refresh_token'])) { echo "Error: Invalid token response\n"; echo "Response: " . $response . "\n"; exit(1); } echo "✓ Tokens received successfully\n"; echo "Access Token: " . substr($token_data['access_token'], 0, 20) . "...\n"; echo "Refresh Token: " . substr($token_data['refresh_token'], 0, 20) . "...\n\n"; // Step 3: Get Organization Info echo "Step 3: Getting organization information...\n"; echo "-----------------------------------------\n"; $org_url = 'https://www.zohoapis.com/crm/v2/org'; $ch = curl_init($org_url); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Zoho-oauthtoken ' . $token_data['access_token'] ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $org_response = curl_exec($ch); $org_data = json_decode($org_response, true); curl_close($ch); if (isset($org_data['org'][0])) { $org = $org_data['org'][0]; echo "✓ Organization found\n"; echo "Name: " . $org['company_name'] . "\n"; echo "ID: " . $org['id'] . "\n"; echo "Time Zone: " . $org['time_zone'] . "\n\n"; } else { echo "Error: Could not get organization info\n"; echo "Response: " . $org_response . "\n"; } // Step 4: Test Module Access echo "Step 4: Testing module access...\n"; echo "-------------------------------\n"; $modules = ['Campaigns', 'Contacts', 'Invoices']; foreach ($modules as $module) { $module_url = "https://www.zohoapis.com/crm/v2/settings/modules/$module"; $ch = curl_init($module_url); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Zoho-oauthtoken ' . $token_data['access_token'] ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $module_response = curl_exec($ch); $module_data = json_decode($module_response, true); curl_close($ch); if (isset($module_data['modules'][0])) { echo "✓ $module module accessible\n"; } else { echo "✗ $module module not accessible\n"; } } // Step 5: Create configuration file echo "\nStep 5: Creating configuration file...\n"; echo "-------------------------------------\n"; $config_content = "