![](/img/trans.png)
[英]Google_Service_Drive retrieveListOfAllFiles only returning one file
[英]Cannot redeclare class Google_Service_Drive
我正在使用google api客戶端上傳文件進行驅動,運行了google developer網站中給出的快速入門示例以列出運行良好的文件。
為了在特定的父文件夾中插入文件,我正在使用兩個對象(請參見下面的兩行),這將導致以下所示的錯誤。
導致錯誤的行(第二行導致錯誤):
$file = new Google_Service_Drive_DriveFile();
$parent = new Google_Service_Drive_ParentReference();
錯誤:
PHP Fatal error: Cannot redeclare class Google_Service_Drive in C:\xampp\htdocs\driveapi\vendor\google\apiclient\src\Google\Service\Drive.php on line 729
PHP Stack trace:
PHP 1. {main}() C:\xampp\htdocs\driveapi\drive_client.php:0
PHP 2. spl_autoload_call() C:\xampp\htdocs\driveapi\drive_client.php:92
PHP 3. Composer\Autoload\ClassLoader->loadClass() C:\xampp\htdocs\driveapi\drive_client.php:92
PHP 4. Composer\Autoload\includeFile() C:\xampp\htdocs\driveapi\vendor\composer\ClassLoader.php:301
完整代碼:(我僅添加了最后兩行,其余的來自Google開發者網站)
<?php require __dir__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'drivephp');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __dir__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-php-quickstart.json
define('SCOPES', implode(' ', array("https://www.googleapis.com/auth/drive")));
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$parentId = null;
// Print the names and IDs for up to 10 files.
$optParams = array(
// 'pageSize' => 10, 'fields' =>
// "nextPageToken, files(id, name)"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
}
}
$file = new Google_Service_Drive_DriveFile();
$parent = new Google_Service_Drive_ParentReference();
?>
請幫忙。
砧,我猜你遇到了我遇到的同樣的問題。 我按照此頁面上的“快速入門”說明進行操作:
https://developers.google.com/drive/v3/web/quickstart/php#前提條件
使用composer安裝appclient:1. *,然后說:
“下載並解壓縮此zip文件,然后將Drive.php復制到vendor / google / apiclient / src / Google / Service /中,替換現有文件。”
我遇到了同樣的錯誤並變得可疑,因此我檢查了GIT,並查看了該文件的歷史記錄:
https://github.com/google/google-api-php-client/blob/v1-master/src/Google/Service/Drive.php
您會注意到,Drive.php的“ v1-master”版本比2015年10月的先前版本短了近2,000行代碼。
最新版本中缺少“ Google_Service_Drive_ParentReference”的所有代碼(我一直在尋找Google_Service_Drive_Property類)。
我將代碼從GIT存儲庫的歷史記錄(2015年10月9日)中復制了到我的Drive.php文件中,現在可以正常工作了。
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.