php - 谷歌云端硬盘 API v3 PHP : Upload a file Call to undefined method

标签 php google-drive-api google-api-php-client

我已按照 https://developers.google.com/drive/v3/web/quickstart/php 上的快速入门进行操作一切正常。

我可以搜索文件、下载文件,但无法上传文件。

我在这里使用这种方法: https://developers.google.com/api-client-library/php/guide/media_upload

$file = new Google_Service_Drive_DriveFile();
$result = $service->files->insert($file, array(
   'data' => file_get_contents("path/to/file"),
   'mimeType' => 'application/octet-stream',
   'uploadType' => 'media'
));

但我总是收到这样的错误:

Fatal error: Uncaught Error:
Call to undefined method Google_Service_Drive_Files_Resource::insert()

我错过了什么?

最佳答案

插入方法已更改为创建:

https://developers.google.com/drive/v3/reference/files/create

另一个陷阱:$file->setTitle 也不起作用(使用 setName)。

最终的代码如下所示:

$file = new Google_Service_Drive_DriveFile();
$file->setName($filename);
$result = $service->files->create($file, array(
  'data' => file_get_contents("path/to/file"),
  'mimeType' => 'application/octet-stream',
  'uploadType' => 'media'
));

关于php - 谷歌云端硬盘 API v3 PHP : Upload a file Call to undefined method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485788/

相关文章:

具有多个条件的 PHP If 语句

javascript - 使用 mySQL 数组对象作为 int

android - 无法为 Google 云端硬盘创建 token 目录

python - 为 Google 云端硬盘中上传的私有(private)文件生成可下载链接

iOS-在 Google Drive Api 上使用上传图片(错误 "Assertion failure in: GTLServer...")

google-api - oauth token 的最大大小

php - 如何改进全文搜索的 mysql 查询?

php - 客户端无权使用此方法检索访问 token - Google calendar php api

php - 如何在 php 中的 google analytic api v4 中使用不完全精确的运算符创建动态段?

javascript - 在动态 php 页面中使用灯箱