如何使用PHP Google Drive API在Google Drive上上传文件

恩奈

我想使用php API在我的Google云端硬盘中添加文件。我在Google控制台以及Google Admin中正确设置了服务帐户权限。我的代码如下:

$client = new Google_Client();
$client->setAuthConfig('path.json');
$client->addScope('https://www.googleapis.com/auth/drive');

$service = new Google_Service_Drive($client);

//Insert a file
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'test.txt'));
$content = file_get_contents('test.txt');
$file = $service->files->create($fileMetadata, array(    'data' => $content, 'mimeType' => 'text/plain', 'uploadType' => 'multipart', 'fields' => 'id'));
print($file->id);

它返回id:1otqEi5qaJ8SDjcPMIFOWtMQVPXZKlaSb但是该文件不在我的驱动器上...

感谢帮助...

戴姆敦

服务帐户不是您。服务帐户具有其自己的Google云端硬盘帐户。该文件正在上传到其Google云端硬盘帐户。

如果您希望它上传到您的Google云端硬盘帐户,请使用该服务帐户的电子邮件地址,并在其中与您的Google云端硬盘帐户共享目录。然后,在上载文件时,获取该文件夹的文件ID /驱动器ID,并在文件元数据中将其设置为父文件,它将被上传到您的驱动器帐户而非服务帐户。

请记住,在升级文件后,请该服务帐户向您授予对该文件的权限,该文件将归服务帐户所有。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Google Drive Java API v3在Google Drive上复制文件?

使用Google Drive API上传文件夹中的文件

Google Drive PHP API-简单文件上传

使用Google-Drive-API移动文件

使用Google Drive Api上传图像文件

如何使用Google Drive API上传文件?

如何使用Google Drive API托管或流式传输在Google Drive中上传的视频

使用Google Drive API与特定用户共享上传的文件

Google Drive API:如何从Google Drive下载文件?

使用Drive API将图片上传到Google Drive

如何使用Google Drive API和Python将文件上传到特定文件夹?

如何使用Google Drive API将文件上传到Google Drive?

使用Google Drive API上传PDF文件-Google Drive Web Client中没有预览

Google Drive PHP API空上传的文件

使用Google Drive API在指定的Google Drive中上传文件

在Azure上,Google Drive Api文件上传失败,返回null

使用Google Drive API获取文件大小

使用 Google Drive API 时不显示上传的文件

如何使用 gdrive 将文件上传到 Google Drive 目录?

我们如何在 Google Drive API 上上传多个文档文件

如何使用 Google Drive API V3 将文件上传到 Group Drive(共享驱动器)?

使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置?

文件上传到 Google Drive

使用 react-native-google-drive-api-wrapper 将文件上传到 Google Drive 时出现 403

使用 Google Apps 脚本将文件上传到 Google Drive

如何使用 Google Drive API 上传我的 Drive (PHP) 中的文件?

如何使用python请求使用google drive API上传和删除文件

如何使用python google drive api将文件夹上传到共享的google drive?

使用 pydrive 或 google drive api 在 google drive 中复制文件