I have a Google workspace account, and I am creating a php panel to manage the team drives, I can already create the drive and add users, the problem is when I have to remove the user from the team drive, I am not able to.
When I create the drive my user is already in the drive as an administrator what I want to do is add two users and remove mine, from the Google drive panel this is possible, but I am not able to remove my user with the api.
//Create the team drive
$requestId = $this->user->id.time();
$client = new ApiGoogleDrive(1000);
try {
$teamDrive = $client->createDrive($requestId , $data["name"]);
//var_dump($teamDrive);
//saves the data of the created drive in the database
$myDrive->id_user = $this->user->id;
$myDrive->id_drive = $teamDrive->getId();
$myDrive->request_id = $requestId;
$myDrive->email = $data["email"];
$myDrive->name = $data["name"];
if (!$myDrive->save()){
$json["message"] = $this->message->error("Oops! there was a failure creating the drive.")->render();
echo json_encode($json);
return;
}
}catch (Exception $exception){
var_dump($exception);
return;
}
try {
/**
* add user
* reference:https://stackoverflow.com/a/55720522/18215077 | https://developers.google.com/drive/api/v3/reference/permissions
* Role: organizer, fileOrganizer, writer, commenter, reader
*/
$service = $client->ServiceDrive();
$postBody = new Google_Service_Drive_Permission();
$postBody->setKind('drive#permission');
$postBody->setEmailAddress($data["email"]);
$postBody->setType("user");
//$postBody->setRole("organizer"); // Administrador
$postBody->setRole("fileOrganizer");
$optParams = [
'supportsTeamDrives' => true
];
$drive_id = $teamDrive->getId();
try {
$service->permissions->create($drive_id, $postBody, $optParams);
//$service->permissions->create($drive_id, $apiPostBody, $optParams);
} catch (Google_Service_Exception $e) {
//return abort($e->getCode());
var_dump($e->getCode());
}
}catch (Exception $exception){
var_dump($exception);
}
But I don't even know where to start when it comes to removing the user from the team drive, could someone show me some example code.
My Answer:
>service->files->create($drive, ['fields' => 'id']);__//Add users to the team drive__ $permission = new Google_Service_Drive_Permission([__ 'type' => 'user',__ 'role' => 'writer',__ 'emailAddress' => 'user1@example.com'__ ]);__ $this->service->permissions->create($drive->id, $permission);__ $permission = new Google_Service_Drive_Permission([__ 'type' => 'user',__ 'role' => 'writer',__ 'emailAddress' => 'user2@example.com'__ ]);__ $this->service->permissions->create($drive->id, $permission);__//Remove user from the team drive__ $permissionId = 'user@example.com'; // Replace with the email address of the user you want to remove__ $this->service->permissions->delete($drive->id, $permissionId);__Make sure you have the necessary permissions to remove users from the team drive. You may need to be an administrator or have the necessary permissions set in the Google Workspace admin console.
Rate this post
3 of 5 based on 1586 votesComments