3

I'm creating a PHP script that will create folders within a Google Drive account. Once the folder is created I will then then share it using the permissions, with other users of my google apps domain.

I've created this part successfully, now I need to add a function that can be done througth the UI, but I can't find a way to do via api: I want to add the folder into the my files section of an users (possibly in a sub folder) Throught the UI can be done going to the "files shared with me" section, right clicking and selecting "add to my file"

How can i do that programmatically? There's a way?

EDIT

I want to do this programmatically in the users directory

https://i.stack.imgur.com/II3Q6.png

at the moment, I can create file and folder in the users directory, only I don't know how to add a folder shared from an other users

Mix Kira
  • 107
  • 1
  • 8
  • If you want the folder in the my files section. Then the user will have to authenticate you using Oauth2. Then you will be able to create the folder directly on there drive account. Files shared with me section is when someone else owns the file like now. – Linda Lawton - DaImTo Oct 15 '15 at 06:58

3 Answers3

2

I've found the answer to my question. I left here, so if someone need it ;)

To add a shared folder of user A, in user B "my files", you need to access the drive api using as user B, get the shared folder (it has the same id that it has in user A instance), and add as parent of that folder the root dir, or a folder inside the user B drive.

$about = $userBServiceInstance->about->get();
$userBRootFolderId = $about->getRootFolderId();

$newParent = new Google_Service_Drive_ParentReference();
$newParent->setId($userBRootFolderId);

$userBServiceInstance->parents->insert($folderId, $newParent);
Mix Kira
  • 107
  • 1
  • 8
0

I don't know if I understood your question correctly but in order to create a folder in the root of Google Drive programmatically, you may want to use the following code. In Google Drive, a folder is a special file type (application/vnd.google-apps.folder that is), see their API documentation for more information.

$client = new Google_Client();
// set various parameters, including name, key and secret

$service = new Google_Service_Drive($client);
$about = $service->about->get();
$rootID = $about->getRootFolderId();

createFolderIfNotExists($service, $rootID, "stackfolder");

// creates a folder in the given rootID or returns the ID if it already exists
function createFolderIfNotExists($service, $rootID, $foldername) {
    $search = "title='{$foldername}' AND '{$rootID}' in parents AND mimeType = 'application/vnd.google-apps.folder' AND trashed != true";
    $parameters = array("q" => $search);

    $files = $service->files->listFiles($parameters);
    if (!empty($files["items"])) {
        $folderID = $files["items"][0]->getId(); // the first element
    } else {
        // create a folder under root
        $file = new Google_Service_Drive_DriveFile();
        $file->setTitle($foldername);
        // To create new folder, we need to set the mime type
        $file->setMimeType('application/vnd.google-apps.folder');
        // set parent folder
        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId($rootID);
        $file->setParents(array($parent));
        $folder = $service->files->insert($file);
        $folderID = $folder->getId();
    }
    return $folderID;
}
Jan
  • 42,290
  • 8
  • 54
  • 79
  • thanks for the answer. Suddently this isn't what I need (I've created the folder and I've shared it with other users) I need to add the folder that I've shared with a user in his "my files" directory – Mix Kira Oct 15 '15 at 14:52
0

it done by adding addParents to the file/folder, with API V3 drive.files.update

PATCH https://www.googleapis.com/drive/v3/files/FILE_ID?addParents=PARENT_ID&key={YOUR_API_KEY}
ewwink
  • 18,382
  • 2
  • 44
  • 54