1

I'm working on a PHP application where users are allowed to create folders on Google Drive via the application. I can't see the folders being created from the application. I've already tried this answer. The service account was set to owner (full access) and I thought that the service account being set to this would allow anyone using the application would at least have writer, commenter, and reader rights. I tried the above answer, but I was only able to print out this:

Google_Service_Drive_Permission Object ( [collection_key:protected] => teamDrivePermissionDetails [allowFileDiscovery] => [deleted] => [displayName] => [domain] => [emailAddress] => [expirationTime] => [id] => 16863121739429784266 [kind] => drive#permission [photoLink] => [role] => writer [teamDrivePermissionDetailsType:protected] => Google_Service_Drive_PermissionTeamDrivePermissionDetails [teamDrivePermissionDetailsDataType:protected] => array [type] => user [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) ) 

SetValue() doesn't seem to be used with Google API v3, instead used the setEmailAddress(). I know that I've accessed the Drive API by looking at the Developer Console Resource Graph and I can see spikes whenever I request a resource.

I'm not getting an error, and I can also see the File/Folder ID, I'm just not seeing the newly created folders in the Drive. What else can it be? Could someone just point me in the right direction?

My code is below:

$client = new Google_Client();
$client->setAccessToken($result);  //$result holds the token, using JWT

$service = new Google_Service_Drive($client);

//Insert a file
$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'window',
    'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
    'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);
$fileId = $file->id;

$newPermission = new Google_Service_Drive_Permission();
$value="createfolder@dummydocs.iam.gserviceaccount.com";
$type="user";
$role="writer";
$newPermission-> setEmailAddress($value);
$newPermission->setType($type);
$newPermission->setRole($role);
try {
    $res= $service->permissions->create($fileId, $newPermission);
     print_r($res); 
} catch (Exception $e) {
     print "An error occurred: " . $e->getMessage();
}
hnewbie
  • 151
  • 1
  • 15
  • I use these 2 packages together to deal with gdrives. http://flysystem.thephpleague.com/docs/ https://github.com/nao-pon/flysystem-google-drive – Tarek Adam Jun 20 '18 at 20:47
  • I'll check those packages out. I just rather not start all over again. – hnewbie Jun 20 '18 at 21:01
  • Can you try [accessing the service account's drive using the API](https://stackoverflow.com/questions/50956663/service-accounts-and-google-drive-creating-a-folder) and check if the created files are there? – ReyAnthonyRenacia Jun 21 '18 at 11:28
  • hol' up....wait, the folders may be created under the service account's drive and not the email its under?!!! How do I access the service account's drive using the API? – hnewbie Jun 21 '18 at 12:21
  • @hnewbie did you get the answer of your last comment i am too struggling with the same issue , how can i access the folder or show it in the drive by service account – Pradeep Prasad Jun 01 '20 at 13:09
  • @Pradeep Prasad No. I was unable to access the drive by service account. I hope you were able to. – hnewbie Jun 10 '20 at 18:50
  • @hnewbie yup, but we have to give a parent id and then create folders in the parent folder in order to access the folders created by service account. I don't know why we cant create the folder in root directory but it is what it is. – Pradeep Prasad Jun 13 '20 at 07:54

0 Answers0