update
This commit is contained in:
98
t2.php
Normal file
98
t2.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
session_start();
|
||||
|
||||
function getClient()
|
||||
{
|
||||
$client = new Google_Client();
|
||||
$client->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 "<b>Connecté comme :</b> " . htmlspecialchars($about->user->displayName) . " (" . htmlspecialchars($about->user->emailAddress) . ")<br><br>";
|
||||
|
||||
// Affiche le select
|
||||
echo '<form method="POST">';
|
||||
echo '<label for="folder_id">Choisissez un répertoire :</label> ';
|
||||
echo '<select name="folder_id" id="folder_id" onchange="this.form.submit()">';
|
||||
echo '<option value="">-- Choisir un dossier --</option>';
|
||||
foreach ($folders as $folder) {
|
||||
$selected = ($selectedFolderId == $folder->id) ? 'selected' : '';
|
||||
echo '<option value="' . htmlspecialchars($folder->id) . '" ' . $selected . '>' . htmlspecialchars($folder->name) . '</option>';
|
||||
}
|
||||
echo '</select>';
|
||||
echo '</form>';
|
||||
|
||||
// Affiche l'ID du dossier sélectionné (pour démo)
|
||||
if ($selectedFolderId) {
|
||||
echo '<h3>ID du dossier sélectionné :</h3><pre>' . htmlspecialchars($selectedFolderId) . '</pre>';
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user