I am trying to upload backup files to GoogleDrive via a cron job.
The main issue I am facing is that I have to use a service account to upload the files because as I understand it, an Oauth service will require the use of a consent screen which obviously isn't an option with cron jobs.
So as it stands now the files are getting uploaded, not to my personal drive, but to a drive that exists "somewhere" attached to the google service account email e.g. something@iam.gserviceaccount.com
. There is no gui (that I can find) where I can manage files uploaded to this ghost account (perhaps if I had gsuite this would be different?). I can however, access it via the api and manage it that way.
What I've ended up doing is making a folder using the api, and putting all the backup files in that parent. The folder itself is created/shared with my personal drive via:
public function makeFolder($folder, $opts = []) {
if (($id = $this->folderExists($folder))) {
return $id;
}
$fileMetadata = new Google_Service_Drive_DriveFile([
'name' => $folder,
'mimeType' => $this->folderMime
]);
$folder = $this->service->files->create($fileMetadata, array('fields' => 'id'));
if (isset($opts['email'])) {
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => 'writer',
'emailAddress' => $opts['email'] //my email/drive something@gmail.com
));
$this->service->permissions->create($folder->id, $userPermission, array('fields' => 'id'));
}
return $folder->id;
}
I can now manage the files in the folder under "shared with me" in my personal drive. I cannot permanently delete files and the general lack of "ownership" isn't ideal.
Is there a more streamlined way of doing this given my parameters? In a perfect world, the api would be like that of dropbox that essentially gives you an access token to use circumventing consent screens entirely.
I am using the latest version of google client api.