调用Google Drive Client的未定义方法

Rap*_*tor 5 php google-client google-drive-api

根据Google页面中提供的示例,我通过Composer安装了Google Client SDK.并尝试使用以下代码将文件上传到Google云端硬盘:

<?php
header('Content-Type: text/plain');
require_once('vendor/autoload.php');

/**
 * Insert new file.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param string $title Title of the file to insert, including the extension.
 * @param string $description Description of the file to insert.
 * @param string $parentId Parent folder's ID.
 * @param string $mimeType MIME type of the file to insert.
 * @param string $filename Filename of the file to insert.
 * @return Google_Service_Drive_DriveFile The file that was inserted. NULL is
 *     returned if an API error occurred.
 */
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
  $file = new Google_Service_Drive_DriveFile();
  $file->setTitle($title);
  $file->setDescription($description);
  $file->setMimeType($mimeType);

  // Set the parent folder.
  if ($parentId != null) {
    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
  }

  try {
    $data = file_get_contents($filename);

    $createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => $mimeType,
    ));

    // Uncomment the following line to print the File ID
    print 'File ID: %s' % $createdFile->getId();

    return $createdFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}

$client_email = 'drive-ocr-service@angelic-tracer-123606.iam.gserviceaccount.com';
$private_key = file_get_contents('key.p12');
$scopes = array(
    'https://www.googleapis.com/auth/drive.file',
    //'https://www.googleapis.com/auth/drive.appdata',
    //'https://www.googleapis.com/auth/drive.apps.readonly'
);
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}

$service = new Google_Service_Drive($client);
// https://developers.google.com/drive/v2/reference/files/insert#examples
$response = insertFile($service, 'Test Image for OCR', 'Image for OCR', null, 'image/jpeg', 'test1.jpg');
if($response === NULL) {
    echo 'Upload failed' . PHP_EOL;
} else {
    var_dump($response);
}
?>
Run Code Online (Sandbox Code Playgroud)

insertFile()函数是从示例页面复制的,但是当我运行脚本时,它显示:

致命错误:调用未定义的方法Google_Service_Drive_DriveFile :: setTitle()

在安装过程中有什么我错过的吗?

Dim*_*rab 9

而不是$file->setTitle($title); 使用$file->setName($filename);

编辑2

Insert()方法正在取代create(),

使用create()而不是insert()

所以基本上新的代码将是(未经过测试),

 $createdFile = $service->files->create($file, array(
      'data' => $data,
      'mimeType' => $mimeType,
    ));
Run Code Online (Sandbox Code Playgroud)

  • 插入()方法正在被create()取代,您可以在此处找到它https://developers.google.com/drive/v3/reference/files/create#request (2认同)