我想將我的Google驅動器中的所有文件列出到我的「DriveFiles.php」文件中,我可以在其中顯示文件及其詳細信息。我是一個初學者,所以完整的代碼將會有所幫助。謝謝。列出Google Drive中的所有文件
我的代碼:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/io/Google_HttpRequest.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
// initialize a client with application credentials and required scopes.
$client = new Google_Client();
$client->setClientId('CLIENT_ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setRedirectUri('REDIRECT_URI');
$client->setScopes(array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'));
$client->setUseObjects(true);
if (isset($_GET['code']))
{
session_start();
print_r($_SESSION);
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$client->setAccessToken($_SESSION['token']);
// initialize the drive service with the client.
$services = new Google_DriveService($client);
retrieveAllFiles($services);
}
if(!$client->getAccessToken()){
$authUrl = $client->createAuthUrl();
echo '<a class="login" href="'.$authUrl.'">Login</a>';
}
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
?>
當我執行的代碼,我得到了以下錯誤:
Fatal error: Uncaught exception 'Google_Exception' with message 'Cant add services after having authenticated' in D:\GT_local\Public\google-api-php-client\src\Google_Client.php:115 Stack trace: #0 D:\GT_local\Public\google-api-php-client\src\contrib\Google_DriveService.php(1258): Google_Client->addService('drive', 'v2') #1 D:\GT_local\Public\quickstart.php(55): Google_DriveService->__construct(Object(Google_Client)) #2 {main} thrown in "FILE_LOCATION(C://google-api-php-client\src\Google_Client.php on line 115)"
我怎樣才能解決這個問題。
我會從最新的客戶端庫開始。你正在使用的那個不再被開發。 https://github.com/google/google-api-php-client – DaImTo 2014-11-04 09:47:51