Google drive api add shared folder to my files

2.6k Views Asked by At

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

3

There are 3 best solutions below

1
On

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;
}
0
On

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);
0
On

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}