setAuthConfig('credentials.json'); $client->addScope(Google_Service_Drive::DRIVE); $client->setRedirectUri('https://ms1crmdev.progiweb.net/t2.php'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); if (isset($token['error'])) { echo "Erreur Google: " . htmlspecialchars($token['error_description'] ?? $token['error']); exit; } $_SESSION['access_token'] = $token; header('Location: ' . filter_var($client->getRedirectUri(), FILTER_SANITIZE_URL)); exit; } if (isset($_SESSION['access_token']) && is_array($_SESSION['access_token']) && isset($_SESSION['access_token']['access_token']) ) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); exit; } if ($client->isAccessTokenExpired()) { unset($_SESSION['access_token']); $authUrl = $client->createAuthUrl(); header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); exit; } return $client; } $parentFolderId = '1BJPxQ2XPWqBRYxINgI-9TKJ_iPz7Dltb'; // Dossier parent $driveId = '0AChhcY8763myUk9PVA'; $client = getClient(); $driveService = new Google_Service_Drive($client); $selectedFolderId = $_POST['folder_id'] ?? null; // Récupère SEULEMENT les dossiers enfants $query = sprintf( "'%s' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'", $parentFolderId ); $folders = []; $pageToken = null; do { $params = [ 'q' => $query, 'fields' => 'nextPageToken, files(id, name, mimeType)', 'supportsAllDrives' => true, 'includeItemsFromAllDrives' => true, 'corpora' => 'drive', 'driveId' => $driveId, 'pageSize' => 1000, ]; if ($pageToken) { $params['pageToken'] = $pageToken; } $results = $driveService->files->listFiles($params); foreach ($results->files as $file) { $folders[] = $file; } $pageToken = $results->getNextPageToken(); } while ($pageToken); // Debug : Affiche le compte connecté $about = $driveService->about->get(['fields' => 'user']); echo "Connecté comme : " . htmlspecialchars($about->user->displayName) . " (" . htmlspecialchars($about->user->emailAddress) . ")

"; // Affiche le select echo '
'; echo ' '; echo ''; echo '
'; // Affiche l'ID du dossier sélectionné (pour démo) if ($selectedFolderId) { echo '

ID du dossier sélectionné :

' . htmlspecialchars($selectedFolderId) . '
'; } ?>