tutorial google for files create php google-api google-drive-sdk google-oauth google-api-php-client

php - for - API de Google Drive: carga y exporta archivos



google drive api upload file (2)

Lo tengo funcionando. Creo que hubo algo con la definición de los ámbitos. Por cierto, encontré algunos ejemplos que fueron muy útiles AQUÍ El código de trabajo es:

<?php date_default_timezone_set(''Europe/Sofia''); include_once __DIR__ . ''/../vendor/autoload.php''; include_once "templates/base.php"; echo pageHeader("File Upload - Uploading a simple file"); /************************************************* * Ensure you''ve downloaded your oauth credentials ************************************************/ if (!$oauth_credentials = getOAuthCredentialsFile()) { echo missingOAuth2CredentialsWarning(); return; } /************************************************ * The redirect URI is to the current page, e.g: * http://localhost:8080/simple-file-upload.php ************************************************/ $redirect_uri = ''http://'' . $_SERVER[''HTTP_HOST''] . $_SERVER[''PHP_SELF'']; $client = new Google_Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); $service = new Google_Service_Drive($client); // add "?logout" to the URL to remove a token from the session if (isset($_REQUEST[''logout''])) { unset($_SESSION[''upload_token'']); } /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the * Google_Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET[''code''])) { $token = $client->fetchAccessTokenWithAuthCode($_GET[''code'']); $client->setAccessToken($token); // store in the session also $_SESSION[''upload_token''] = $token; // redirect back to the example header(''Location: '' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION[''upload_token''])) { $client->setAccessToken($_SESSION[''upload_token'']); if ($client->isAccessTokenExpired()) { unset($_SESSION[''upload_token'']); } } else { $authUrl = $client->createAuthUrl(); } /************************************************ * If we''re signed in then lets try to upload our * file. For larger files, see fileupload.php. ************************************************/ if ($_SERVER[''REQUEST_METHOD''] == ''POST'' && $client->getAccessToken()) { // CREATE A NEW FILE $file = new Google_Service_Drive_DriveFile(array( ''name'' => ''sample'', ''mimeType'' => ''application/vnd.google-apps.presentation'' )); $pptx = file_get_contents("sample.docx"); // read power point pptx file //declare opts params $optParams = array( ''uploadType'' => ''multipart'', ''data'' => $pptx, ''mimeType'' => ''application/vnd.openxmlformats-officedocument.wordprocessingml.document'' ); //import pptx file as a Google Slide presentation $createdFile = $service->files->create($file, $optParams); //print google slides id //print "File id: ".$createdFile->id; $optParams2 = array( ''fileId'' => $createdFile->id, ''mimeType'' => ''application/pdf'' ); $response = $service->files->export($createdFile->id, ''application/pdf'', array( ''alt'' => ''media'' )); //print_r($response); $content = $response->getBody()->getContents(); ///rint_r($content); $data = $content; file_put_contents(''test_ppt.pdf'',$data); } ?> <div class="box"> <?php if (isset($authUrl)): ?> <div class="request"> <a class=''login'' href=''<?= $authUrl ?>''>Connect Me!</a> </div> <?php elseif($_SERVER[''REQUEST_METHOD''] == ''POST''): ?> <div class="shortened"> <p>Your call was successful! Check your drive for the following files:</p> <ul> </ul> </div> <?php else: ?> <form method="POST"> <input type="submit" value="Click here to upload two small (1MB) test files" /> </form> <?php endif ?> </div> <?= pageFooter(__FILE__) ?>

Si desea convertir a otros formatos, simplemente cambie los tipos Mime, siguiendo las referencias: AQUÍ y AQUÍ

Necesito subir un archivo a través de Google Drive y luego exportarlo como formato diferente. Por ejemplo, cargue DOCX y expórtelo a PDF. He estado siguiendo el REST quickstart y las guías de carga de archivos. Después de ejecutar el código, obtengo un error:

Error fatal: excepción no detectada ''Google_Service_Exception, "mensaje": "Permiso insuficiente"

Es un problema con los permisos, pero no sé cómo solucionarlo. Aquí está el código que uso:

date_default_timezone_set(''Europe/Sofia''); require_once __DIR__ . ''/vendor/autoload.php''; define(''APPLICATION_NAME'', ''Drive API PHP Quickstart''); define(''CREDENTIALS_PATH'', ''~/.credentials/drive-php-quickstart.json''); define(''CLIENT_SECRET_PATH'', __DIR__ . ''/client_secret.json''); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/drive-php-quickstart.json define(''SCOPES'', implode('' '', array( Google_Service_Drive::DRIVE_METADATA_READONLY) )); //i''ve tried to change the scope to ::DRIVE, but still get the same error if (php_sapi_name() != ''cli'') { throw new Exception(''This application must be run on the command line.''); } /** * Returns an authorized API client. * @return Google_Client the authorized client object */ function getClient() { $client = new Google_Client(); $client->setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES); $client->setAuthConfig(CLIENT_SECRET_PATH); $client->setAccessType(''offline''); // Load previously authorized credentials from a file. $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); if (file_exists($credentialsPath)) { $accessToken = json_decode(file_get_contents($credentialsPath), true); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:/n%s/n", $authUrl); print ''Enter verification code: ''; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); // Store the credentials to disk. if(!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, json_encode($accessToken)); printf("Credentials saved to %s/n", $credentialsPath); } $client->setAccessToken($accessToken); // Refresh the token if it''s expired. if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); } return $client; } /** * Expands the home directory alias ''~'' to the full path. * @param string $path the path to expand. * @return string the expanded path. */ function expandHomeDirectory($path) { $homeDirectory = getenv(''HOME''); if (empty($homeDirectory)) { $homeDirectory = getenv(''HOMEDRIVE'') . getenv(''HOMEPATH''); } return str_replace(''~'', realpath($homeDirectory), $path); } // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Drive($client); // Print the names and IDs for up to 10 files. $optParams = array( ''pageSize'' => 10, ''fields'' => ''nextPageToken, files(id, name)'' ); $results = $service->files->listFiles($optParams); if (count($results->getFiles()) == 0) { print "No files found./n"; } else { print "Files:/n"; foreach ($results->getFiles() as $file) { printf("%s FILE_ID(%s)/n", $file->getName(), $file->getId()); } } //The error I get is in this block: $fileMetadata = new Google_Service_Drive_DriveFile(array( ''name'' => ''photo.jpg'')); $content = file_get_contents(''photo.jpg''); $file = $service->files->create($fileMetadata, array( ''data'' => $content, ''mimeType'' => ''image/jpeg'', ''uploadType'' => ''multipart'', ''fields'' => ''id'')); printf("File ID: %s/n", $file->id);

Editar: Olvidé mencionar que el código funciona hasta el último bloque, enumera los archivos correctamente y luego en las últimas 10 filas arroja el error


Los ámbitos definen el alcance del acceso que solicita a un usuario. Has autenticado tu código con los siguientes permisos.

Google_Service_Drive :: DRIVE_METADATA_READONLY

Lo que le da acceso de solo lectura. La siguiente es una lista de ámbitos disponibles para la API de Google api.

https://www.googleapis.com/auth/drive Ver y administrar los archivos en su Google Drive

https://www.googleapis.com/auth/drive.appdata Ver y administrar sus propios datos de configuración en su Google Drive

https://www.googleapis.com/auth/drive.file Ver y administrar archivos y carpetas de Google Drive que haya abierto o creado con esta aplicación

https://www.googleapis.com/auth/drive.metadata Ver y administrar metadatos de archivos en su Google Drive

https://www.googleapis.com/auth/drive.metadata.readonly Ver metadatos para archivos en su Google Drive

https://www.googleapis.com/auth/drive.photos.readonly Ver las fotos, videos y álbumes en Google Fotos

https://www.googleapis.com/auth/drive.readonly Ver los archivos en su Google Drive

https://www.googleapis.com/auth/drive.scripts Modificar el comportamiento de las secuencias de comandos de Google Apps Script

Probablemente vaya con el primero https://www.googleapis.com/auth/drive.file . Solo estoy adivinando aquí pero para PHP puede ser algo así como Google_Service_Drive::DRIVE_FILE