使用服务帐户为用户创建Team Drive

时间:2019-03-26 13:01:02

标签: google-api google-drive-api google-api-php-client google-drive-team-drive

我正在创建一个管理面板,该面板使用Google API和PHP SDK创建一个G Suite用户帐户。我已经完成了创建用户的部分,但是现在我想知道如何为新创建的用户创建Team Drive?我看不到可以在其中添加新创建的用户ID(例如电子邮件)的参数。我想念什么吗?

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

下面的代码可以创建一个团队驱动器,但是它将被添加到创建该服务帐户的G Suite帐户中。

$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']) );

1 个答案:

答案 0 :(得分:0)

我通过在创建团队驱动器后附加驱动器权限来解决此问题。不要忘记将可选参数supportTeamDrives设置为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());
    }