我想從php腳本訪問我的Google雲端硬盤文件,該腳本將在cron作業中運行,但我看不到我的文件。 我正在使用Google Developers控制檯中設置的服務帳戶,該帳戶不需要與用戶(我)進行授權交互。 這是我的代碼:Google雲端硬盤訪問服務帳戶
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
$client_id = '260186860844-kg9uiqe7pjms3s54gabqfnph0iamjdjn.apps.googleusercontent.com';
$service_account_name = '[email protected]account.com';
$key_file_location = 'privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Whatever");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Drive($client);
$parameters = array(
// 'maxResults' => 10,
);
$dr_results = $service->files->listFiles($parameters);
foreach ($dr_results as $item) {
echo $item->title, "<br /> \n";
}
看來我連接正確(有標記,沒有錯誤提示),但我只得到列出一個單一的文件,這是不是我的驅動器的一部分,並且被命名爲「如何開始使用雲端硬盤「!
我使用最新Google APIs Client Library for PHP,使用PHP 5.4