PHP Google Drive API V2 returns empty file list

I’m working on a Joomla extension that needs to display files from a specific Google Drive directory. The component should work as a background service without user authentication.

The problem is that my API calls always return zero items even though I know there are files in the drive. I’m wondering if my client setup is incorrect or if there’s an issue with how I configured the application in Google Console.

Here’s my current implementation:

// Configuration file contains all Google Console credentials
require_once(dirname(__FILE__) . '/drive/settings.php');
require_once(dirname(__FILE__) . '/drive/google-api/googleClient.php');
require_once(dirname(__FILE__) . '/drive/google-api/services/googleDriveService.php');

$googleClient = new googleClient();
$privateKey = file_get_contents(Settings::PRIVATE_KEY_PATH);
$googleClient->setClientId(Settings::APP_CLIENT_ID);

$googleClient->setUseObjects(true);
$googleClient->setAssertionCredentials(new googleAssertionCredentials(
    Settings::SERVICE_EMAIL,
    array('https://www.googleapis.com/auth/drive'), $privateKey)
);

$driveService = new googleDriveService($googleClient);

try {
    $options = array();
    $options['maxResults'] = $maxItems;

    $fileList = $driveService->files->listFiles($options);

    $documents = $fileList->getItems();
    var_dump($documents);
    $nextToken = $fileList->getNextPageToken();
} catch (googleServiceException $ex) {
    echo "Service error: " . $ex->getCode() . "\n";
    echo "Error details: " . $ex->getMessage() . "\n";    
} catch (googleException $ex) {
    echo "API error: (" . $ex->getCode() . ") " . $ex->getMessage() . "\n";
} catch (Exception $ex) {
    echo "Failed to fetch files";
    echo($ex->getMessage());
}

This is really frustrating and I can’t figure out what’s wrong. Any help would be appreciated.

Note: I used the official Google documentation for service account setup as my reference.

Had this exact problem on a client project. Service accounts can only see files they created or have explicit access to - they don’t get the same view as regular users. Your service account starts with zero Drive access.

Just share the folder directly with your service account email (looks like [email protected]). Add it as a collaborator through the Drive web interface - viewer permissions work fine.

Also double-check that Drive API is enabled in Google Cloud Console and your credentials file path is right. Took me hours to figure this out when I hit the same wall.

This is probably a Google Drive service account permissions issue. I ran into the same thing - my service account was set up correctly but couldn’t see the target folder. Service accounts work separately from your personal Drive, even when authentication looks fine.

Share the folder with your service account email, but also verify the API calls are actually authenticating. Add some debug output to check if $googleClient->getAccessToken() returns a valid token after you set the assertion credentials. Sometimes auth fails silently without throwing an exception.

Also check if you’re hitting a Team Drive instead of regular Drive folders - those need different API endpoints and permissions. Empty responses usually mean successful auth but no accessible resources, not auth failure.

check your scopes - sometimes the drive scope doesn’t give full access to all folders. try adding ‘https://www.googleapis.com/auth/drive.readonly’ if you just need read access. also make sure your service account key file isn’t corrupted. redownload it from the console if you’re not sure.