setApplicationName('Google Drive API PHP Quickstart'); $client->setScopes(Drive::DRIVE_FILE); $client->setAuthConfig(__DIR__.'/credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); $tokenPath = 'token.json'; if (file_exists($tokenPath)) { $accessToken = json_decode(file_get_contents($tokenPath), true); $client->setAccessToken($accessToken); } if ($client->isAccessTokenExpired()) { if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); } else { $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); $client->setAccessToken($accessToken); if (!file_exists(dirname($tokenPath))) { mkdir(dirname($tokenPath), 0700, true); } file_put_contents($tokenPath, json_encode($client->getAccessToken())); } } return $client; } // Función para subir archivo a Google Drive function uploadFileToDrive($client, $filePath) { $driveService = new Drive($client); $fileMetadata = new Drive\DriveFile(array( 'name' => basename($filePath) )); $content = file_get_contents($filePath); $file = $driveService->files->create($fileMetadata, array( 'data' => $content, 'mimeType' => mime_content_type($filePath), 'uploadType' => 'multipart', 'fields' => 'id' )); printf("File ID: %s\n", $file->id); }