PHP Google Drive API V2 returns empty file list despite correct setup

I’m working on a Joomla component that needs to display files from a specific Google Drive directory. The component runs as a background service without user authentication, so I’m using service account credentials instead of OAuth flow.

The problem is that my API calls always return zero files, even though I know there are documents in the drive. I’m wondering if my client configuration is incorrect or if there’s an issue with how I registered my app in Google Console.

require_once(dirname(__FILE__) . '/drive-api/settings.php');
require_once(dirname(__FILE__) . '/drive-api/google-v2/ClientAPI.php');
require_once(dirname(__FILE__) . '/drive-api/google-v2/services/DriveAPI.php');

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

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

$driveAPI = new DriveAPI($apiClient);

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

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

    $fileItems = $fileList->getItems();
    print_r($fileItems); // Always empty array
    $nextToken = $fileList->getNextPageToken();
} catch (ServiceException $ex) {
    echo "Service Error: " . $ex->getCode() . "\n";
    echo "Details: " . $ex->getMessage() . "\n";    
} catch (ClientException $ex) {
    echo "Client Error: (" . $ex->getCode() . ") " . $ex->getMessage() . "\n";
} catch (Exception $ex) {
    echo "Failed to fetch files: " . $ex->getMessage();
}

This is really frustrating because I followed the official documentation for service accounts but still get no results. Any ideas what might be wrong?

Your service account can’t access those files. When you upload through Google Drive’s interface, they belong to your personal account - not the service account. The service account has its own separate Drive space.

I hit this exact problem building a document system. Here’s what fixed it: I created a folder in my regular Drive, shared it with the service account email (the @yourproject.iam.gserviceaccount.com one), then uploaded files there. The API started working right away.

You can also try adding q: 'sharedWithMe' to your options array. This shows which files the service account actually sees vs. what you think it should access.

I’ve hit this exact problem before - it’s almost always permissions, not your code. Service accounts can only access files shared with them directly or files they created.

Quick fix: share your Google Drive folder with your service account email (looks like [email protected]). Share it like you would with any regular user.

Side note: V2 API is deprecated. Google wants everyone on V3 now.

Honestly though, after years of fighting Google Drive API permissions and auth nightmares, I moved everything to Latenode. It handles all the Google API mess with simple drag-and-drop workflows.

You can automate file syncing, processing, and listing without writing any PHP auth code. Plus it connects to hundreds of other services for further processing.

Way less debugging than managing service account credentials yourself.

Check if your service account has domain-wide delegation enabled in the admin console. Also make sure the Drive folder isn’t in someone’s personal drive - the service account can’t see those. I had the same issue last month and it turned out my files were in my personal folder instead of a shared workspace.