0

I'm currently doing a PHP script on a web app to scrap my drive : By opening a PHP in my browser, I have a script based on examples provided by Google (https://github.com/google/google-api-php-client).

This script simply proceeds to a Files List and copy the files on the server. It's working fine, but I want my Drive files to be synchronized with my server files. I have to run my PHP in my browser every time I want to copy again my files. Here are my questions about that :

  1. Could I make a PHP script which would be executed programmatically by CRON everyday ? At the moment the PHP opened in my browser needs an authentication (If no cookie of Google account already signed in is detected, I first need to login). I'd like to authorize my Google App once on my account to be able to scrap the drive every day.
  2. Can I apply this script on multiple Google Accounts (Like having a list of G adresses or tokens, and the script runs for each one). The web app I'm doing is for a dizain of persons, and I'd like to synchronize their drives files with my server.

Thank you in advance for any answer. I don't know if this is very useful, but here is my code :

function retrieveAllFiles($service, $parameters) {
  $results = array();
  $pageToken = NULL;
  do{
    try {
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);
      $results = array_merge($results, $files->getFiles());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "Une erreur s'est produite : " . $e->getMessage();
      $pageToken = NULL;
    }
  }while($pageToken);
  return $results;
}

// Function to get a list of files id
function scrapDrive($service, $excluded) {

  $final_files = array();
  $query = "";
  if(sizeof($excluded) == 0){
    $query = "name = '--------'";
  }else{
    $query = "name = '".$excluded[0]."'";
  }
  // I just do a custom query if the user has chosen to exclude personal folders from his drive
  foreach ($excluded as $folder_name) {
    $query .= " or name ='".$folder_name."'";
  }

  // Get folders ID
  $excluded_folders = retrieveAllFiles($service, array('q' => $query));
  $excluded_folders_id = array();

  foreach ($excluded_folders as $folder) {
      array_push($excluded_folders_id, $folder['id']);
  }

  $usermail = getUserMail($service);
  // Getting only files from last month
  $artworks_query = "modifiedTime > '".date("Y-m-d\TH:i:sP",strtotime("-1 month"))."'";

  // Only getting images
  $artworks_query .= " and mimeType contains 'image/' and '".$usermail."' in owners";

  foreach ($excluded_folders_id as $id) {
    $artworks_query .= " and not '".$id."' in parents";
  }

  $artworks = retrieveAllFiles($service, array('q' => $artworks_query));

  foreach ($artworks as $artwork) {
    $final_artworks[$artwork['id']] = $artwork['name'];
  }
  return $final_artworks;

}



$excluded = getExcludedFolders($_SESSION['id']);
$artworks = scrapDrive($service, $excluded);
$nb_of_artworks = sizeof($artworks);

$i = 1;
  // Copy each file
  foreach ($artworks as $artwork_id => $artwork_name) {
      $response = $service->files->get($artwork_id, array(
        'alt' => 'media' ));
      $content = $response->getBody()->getContents();
      file_put_contents("folder/".$artwork_id.'_'.$artwork_name, $content);
    }
    $i++;
  }

Thank you in advance for any help ! :-)

DaImTo
  • 72,534
  • 21
  • 122
  • 346
saperlipopette
  • 1,349
  • 8
  • 22
  • see https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic – pinoyyid Aug 05 '17 at 08:00
  • Hello, thank for your reply. I've already seen this post, this is about doing it from oauth playground, not by a script, am I right ? – saperlipopette Aug 05 '17 at 09:37
  • read the full post. it uses oauth playground only to obtain a refresh token.you then embed that token n your script. – pinoyyid Aug 05 '17 at 09:45

1 Answers1

2
  1. Could I make a PHP script which would be executed programmatically by CRON everyday?

Yes you can run a php script in cron. how to run a php script daily with the cron job on Ubuntu os

At the moment the PHP opened in my browser needs an authentication (If no cookie of Google account already signed in is detected, I first need to login).

That is because you are using Oauth2. You could just as easily save your refresh token in a file and then have your script read the refresh token from the file rather than reading it from the cookie. check this

I'd like to authorize my Google App once on my account to be able to scrap the drive every day.

If this is an account that you personally have control of then you should consider using service account authentication rather than Oauth2. check this

Can I apply this script on multiple Google Accounts (Like having a list of G adresses or tokens, and the script runs for each one). The web app I'm doing is for a dizain of persons, and I'd like to synchronize their drives files with my server.

Using Oauth2 you can have a refresh token for as many accounts as you wish. The owner of the account must simply authenticate your application and you will have a refresh token to access it.

DaImTo
  • 72,534
  • 21
  • 122
  • 346
  • At the moment I have a web page, I have to access to my google account manually, and then my script operates in my browser. You mean that I can have a web page where I authenticate my google account, I get the refresh token (which is then stored on my server), and my server can run a php script to access the drive only with the refresh token ? Is this token valable forever (since there is "refresh" in the name) ? – saperlipopette Aug 05 '17 at 19:45
  • Yes all of that is possible – DaImTo Aug 05 '17 at 19:46
  • I'm trying to code this. But let's imagine I first connect with my G account, and I get the refresh token. Will this refresh token be usable even 1 week later ? I saw there was an 1 hour valability – saperlipopette Aug 05 '17 at 20:24
  • Refresh token don't expire you can use it when ever you want to get a new access token which will work for one hour and allow you to access the api in question – DaImTo Aug 05 '17 at 20:28
  • Okay I think I get it. I have a code which simply asks for a google auth and stores the refresh token of the G account. Now on a php script opened by CRON I would have to get this refresh token, to then get an access token which will allow me to do what I want on my drive ? And this server side PHP would contain code like the one I was using ? $client = new Google_Client(); and $client->setAccessType('offline'); and etc... ? – saperlipopette Aug 05 '17 at 20:35