0

I'm using the following code to connect and make my requests to G-API:

require_once '/google-api/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/mySecretFile.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);

$files = $service->files->listFiles()->getFiles();

foreach($files as $file){
    print_r($file); 
}

I tried this code a couple of times, and all of them the result comes with just 1 file, which I believe it's no mine, since it doesn't exist in my drive, so what's happening? There is the image of the only file I'm getting.

enter image description here

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Leo Letto
  • 1,774
  • 1
  • 12
  • 17
  • Try to [list](https://developers.google.com/drive/v3/reference/files/list) your files using the [Try it now](https://developers.google.com/drive/v3/reference/files/list#try-it) if they were present in Gdrive. Then you can refer in this [github post on how to Lists or searches files](https://gist.github.com/bshaffer/9bb2cdccd315880ab52f). Find Lists or searches files as keyword to search in code. – MαπμQμαπkγVπ.0 May 09 '18 at 11:00

1 Answers1

1

You appear to be authenticating with a service account. Think of a service account as a dummy user. Service accounts have their own Google drive account, google calendar and probably a bunch more.

The file you are seeing is probably a file on its drive account. If you want it to have access to your personal drive account then you will have to take the service account email address and share which ever directory you want it to have permission to access with it.

Note: No you cant share root folder

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Got it, really intersting, I didn't read about it in any where ... Anyway, sharing my folders with the service account that I created, can I do everything by using this bot? Like listeing, uploadind...? – Leo Letto May 09 '18 at 12:25
  • you service account will function the same just watch permission if you upload a file make sure to grant your own account access. – Linda Lawton - DaImTo May 09 '18 at 19:41