Pregunta

Is there any way we could create a folder on google drive using PHP? I've been searching all the web for the last 72 hours with no results. What do you think guys? Thanks !

¿Fue útil?

Solución

Creating a Folder:

"In the Drive API, a folder is essentially a file — one identified by the special folder MIME type application/vnd.google-apps.folder. You can create a new folder by inserting a file with this MIME type and a folder title. Do not include an extension when setting a folder title.

To create a subfolder to a particular folder, specify the the correct ID in the parents property of the file. For example, to create the folder "pets" in an "images" folder whose id value is 0ADK06pfg:"

POST https://www.googleapis.com/drive/v2/files
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json
...
{
  "title": "pets",
  "parents": [{"id":"0ADK06pfg"}]
  "mimeType": "application/vnd.google-apps.folder"
}

Source: https://developers.google.com/drive/folder

Examples: https://developers.google.com/drive/examples/php

Otros consejos

Here is an updated method using the Google Drive API v3 and an OAuth access token:

$googleClient = new \Google_Client();
$googleClient->setApplicationName('My App');
$googleClient->setAccessToken(env('GOOGLE_OAUTH_ACCESS_TOKEN'));
$googleClient->setClientId(env('GOOGLE_APP_ID'));
$googleClient->setClientSecret(env('GOOGLE_APP_SECRET'));

if ($googleClient->isAccessTokenExpired()) {
    $googleClient->fetchAccessTokenWithRefreshToken();
}

$api = new \Google_Service_Drive($client);

$file = new \Google_Service_Drive_DriveFile();
$file->setName('Folder Name');
$file->setMimeType('application/vnd.google-apps.folder');

$folder = $api->files->create($file);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top