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

Symfony+React调用Google Drive API下载文件遇404问题求助

Google Drive文件拉取404问题求助

我已查阅多篇文档和论坛,该问题虽已有讨论,但现有方案均无法解决我的问题,可能缺少相关配置,特此求助。

技术栈:

  • PHP:8.2
  • Symfony:6.4
  • ReactJs:18.3

需求很简单:用户点击按钮展示其个人Drive及共享驱动器中的所有图片,我使用react-google-drive-picker可正常获取目标图片。但使用file.id通过后端接口拉取文件到服务器时,始终返回Error 404 File not found,原因不明。

已测试但无效(均返回404)的场景:

  • 我个人Drive中创建的文件
  • 我在共享驱动器中创建的文件
  • 他人在共享驱动器中创建的文件
  • 尝试所有Scope组合及单独使用
  • 操作文件的账号拥有下载权限(尤其是我个人创建的文件)

可正常运行的前端代码(展示Google Drive图片):

import useDrivePicker from 'react-google-drive-picker';

export const Drive = () => {

const CLIENT_ID = "MY_CLIENT_ID";
const API_KEY = "API_KEY"; // 原代码存在引号缺失问题,已补全

const [openPicker] = useDrivePicker();

const handleOpenPicker = () => {
        openPicker({
            clientId: CLIENT_ID,
            developerKey: API_KEY,
            viewId: "DOCS_IMAGES",
            showUploadFolders: true,
            supportDrives: true,
            locale: 'fr',
            // customScopes: ['https://www.googleapis.com/auth/drive'],
            callbackFunction: (data) => {
                if (data.action === 'cancel') {
                    console.log('User clicked cancel/close button')
                }
                if (data.docs) {
                    const file = data.docs[0];
                    console.log(file); // 此处可正常获取文件对象
                }
            },
        })
    }

    return (
        <>
            <button onClick={() => handleOpenPicker()}>Open Picker</button>
        </>
    )
}

无法运行的后端代码(使用相同凭证拉取Google Drive文件):

<?php

use Google\Client;
use Google\Service\Drive;
use Google\Service\Exception;

// .... 
$request = json_decode($this->requestStack->getCurrentRequest()->getContent(), true);
$credentials = "../config/client_secret.json"; // 此文件来自Google控制台创建OAuth客户端ID时的下载文件  
$client = new Client();
$client->setAuthConfig($credentials);
$client->setDeveloperKey("SAME_API_KEY_FRONT_END");
$client->setClientId("SAME_CLIENT_ID_FRONT_END");
$client->addScope([Drive::DRIVE, Drive::DRIVE_FILE, Drive::DRIVE_METADATA, Drive::DRIVE_READONLY]);
$driveService = new Drive($client);
$response = $driveService->files->get($request["file"], [
        'alt' => 'media',
        'supportsAllDrives' => true
    ]
);
dd($response);

内容的提问来源于stack exchange,提问作者Ahmed Kallali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:50:55