0

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.

enter image description here

I am using the latest version of google client api.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • see https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention/19766913#19766913 – pinoyyid Jul 24 '19 at 14:26

1 Answers1

0

Have a look at OAuth refresh tokens. When the user authorizes your app, you receive an access token and a refresh token. The access token is used to authorize API calls, and the refresh token is used to get new access tokens. So this will let you generate an access token every time you run a cron job, but the user will only have to enter authentication details once. The refresh token does expire, but this is usually after a much longer period.

Grant C.
  • 346
  • 1
  • 7