You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何G-Drive无法返回文件列表?附调用G-API的PHP代码

Troubleshooting Your Google Drive API File List Issue

Hey Leo, let's troubleshoot why your Google Drive API call isn't returning any files—this is a super common gotcha with service accounts, so let's go through the most likely fixes step by step:

1. You're accessing the service account's own Drive (not your personal one)

The biggest mistake here is assuming the service account can access your personal Drive files by default. Service accounts have their own isolated Drive storage, which is empty unless you explicitly share files/folders with it.

Fix:

  • Find your service account's email (look in your mySecretFile.json under the client_email field).
  • Go to your personal Google Drive, right-click the file/folder you want to access, select "Share", and paste the service account email. Grant it at least "Viewer" permissions.
  • Re-run your code—you should now see the shared files.

2. Verify the Google Drive API is enabled in your Cloud Project

Even if you have credentials, the API itself might not be enabled for your Google Cloud project:

  • Open the Google Cloud Console for your project.
  • Search for "Google Drive API" in the API library.
  • If it says "Enable", click it—if it says "Manage", it's already enabled.

3. Check your credentials file path and permissions

Your code uses /mySecretFile.json, but:

  • Make sure the file path is correct (test with var_dump(file_exists('/mySecretFile.json')); to confirm it's accessible).
  • Ensure your web server user (e.g., www-data for Apache, nginx for Nginx) has read permissions for the file. Set permissions to 600 (read/write only for the owner) to keep it secure.

4. Add error handling and explicit list parameters

Your current code doesn't catch API errors, and the default listFiles() call might be returning empty due to pagination or missing fields. Update your code with error handling and explicit parameters:

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);

try {
    // Request up to 10 files, only return ID and name for clarity
    $response = $service->files->listFiles([
        'pageSize' => 10,
        'fields' => 'files(id,name)'
    ]);
    $files = $response->getFiles();

    if (empty($files)) {
        echo "No files found—make sure you've shared files with the service account!";
    } else {
        foreach($files as $file){
            echo "File ID: {$file->getId()}, Name: {$file->getName()}\n";
        }
    }
} catch (Google_Service_Exception $e) {
    echo "API Error: " . $e->getMessage();
} catch (Exception $e) {
    echo "General Error: " . $e->getMessage();
}

This will show you clear error messages if something's wrong (e.g., invalid credentials, missing scopes) and make it easier to see if files are being returned.

5. Confirm your scope is correct

Your current scope (https://www.googleapis.com/auth/drive) is valid for full Drive access, but if you only need read access, you can use https://www.googleapis.com/auth/drive.readonly to follow the principle of least privilege. This isn't the issue here, but it's a good practice.


内容的提问来源于stack exchange,提问作者Leo Letto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:23:07