Google Drive API - 尝试更新文件时,调用非对象上的成员函数

时间:2013-11-14 17:58:38

标签: php google-drive-api

我正在尝试使用PHP访问和更新Google云端硬盘中的文件。一切顺利,直到我尝试调用$ file_to_update-> setTitle(“NEW TITLE”)。

我可以下载文件的元数据,但我无法更新任何内容。

require_once 'google-api-php-client/Google_Client.php';
require_once 'google-api-php-client/contrib/Google_DriveService.php';



$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('');
$client->setScopes(array(''));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);


retrieveAllFiles($service);

function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;

  do {
try {
  $parameters = array();
  if ($pageToken) {
    $parameters['pageToken'] = $pageToken;
  }
  $files = $service->files->listFiles($parameters);


  $fileIDs = array();

  $file = ($files[items]);
  foreach($file as $f){
    array_push($fileIDs, $f["id"]);
    print $f["id"]."\n";
  }

  $str = $fileIDs[1];

  $file_to_update = $service->files->get($str);

  $file_to_update->setTitle("NEW TITLE");

} catch (Exception $e) {
  print "An error occurred: " . $e->getMessage();
  $pageToken = NULL;
}
} while ($pageToken);
 return $result;
}

1 个答案:

答案 0 :(得分:1)

您对$ service-> files-> get($ str)的调用不会返回对象。

如果你检查功能:

public function get($fileId, $optParams = array()) {
  $params = array('fileId' => $fileId);
  $params = array_merge($params, $optParams);
  $data = $this->__call('get', array($params));
  if ($this->useObjects()) {
    return new Google_DriveFile($data);
  } else {
    return $data;
  }
}

它会检查您是否要使用对象:

  

$这 - > useObjects()

您需要配置' use_objects'真的'在你的api config.php文件中,它被设置为' false'默认情况下。

  

' use_objects' =>假,