James
James

Reputation: 21

Create Team Drive for a user using a service account

I am creating an admin panel that creates a G Suite user account using Google API with a PHP SDK. I am done with the part in creating a user but now I am left wondering how can I create a Team Drive for the newly created user? I can't see a parameter where I can add the newly created user's id like email. Am I missing something?

https://developers.google.com/drive/api/v3/reference/teamdrives/create

Code below can create a Team Drive but it will be added into the G Suite account that created the service-account.

$service = new \Google_Service_Drive($this->client($scopes));

$metadata = new Google_Service_Drive_TeamDrive();
$metadata->setName('My New Drive');

do {
   $requestId = Uuid::uuid4()->toString();
   try {
        $resp = $service->teamdrives->create($requestId, $metadata);
   } catch (Google_Service_Exception $e) {
        $resp['statusCode'] = $e->getCode();
        $resp['errors'] = $e->getErrors();
        $resp['message'] = $e->getMessage();
   }
} while ( isset($resp['statusCode']) );

Upvotes: 0

Views: 269

Answers (1)

James
James

Reputation: 21

I solved this by attaching drive permission right after creation of the Team Drive. Don't forget to set the optional parameter supportsTeamDrives to true

    $service = new \Google_Service_Drive($this->client($scopes));

    $postBody = new \Google_Service_Drive_Permission();
    $postBody->setKind('drive#permission');
    $postBody->setEmailAddress($email);
    $postBody->setType($type);
    $postBody->setRole($role);
    $optParams = [
        'supportsTeamDrives' => true
    ];

    try {
         $resp = $service->permissions->create($drive_id, $postBody, $optParams);
    } catch (Google_Service_Exception $e) {
         return abort($e->getCode());
    }

Upvotes: 2

Related Questions