I believe your goal is as follows.
- You want to create a new Google Spreadsheet and share it with an email address.
- You want to achieve this using PHP.
Although, unfortunately, I'm not sure about your current script from your question, when you want to achieve this using googleapis for php, how about the following sample script?
Sample script:
$client = ###; // Please use your client from the service account.
$name = "sampe Spreadsheet name"; // Please set Spreasdheet name.
$email = "###"; // Please set email address you want to share.
$service = new Google_Service_Drive($client);
// 1. Create a new Spreadsheet.
$mimeType = "application/vnd.google-apps.spreadsheet";
$f = new Google_Service_Drive_DriveFile();
$f->setName($name);
$f->setMimeType($mimeType);
// $f->setParents(["###folderId###"]); // If you want to put the created Spreadsheet to the specific folder, please use this.
$spreadsheet = $service->files->create($f);
$spreadsheet_id = $spreadsheet->getId();
// 2. Share the created Spreadsheet with an email address.
$permission = new Google_Service_Drive_Permission();
$permission->setEmailAddress($email);
$permission->setType('user');
$permission->setRole('writer');
$service->permissions->create($spreadsheet_id, $permission);
// 3. Show URL of created Spreadsheet.
$spreadsheet_url = "https://docs.google.com/spreadsheets/d/" . $spreadsheet_id . "/edit";
print($spreadsheet_url);
When this script is run, a new Google Spreadsheet is created to the root folder, and the created Spreadsheet is shared with the email address as the writer.
In this case, as a sample, the scope of Google_Service_Drive::DRIVE
was used.
References: