我已經在服務類型憑證在谷歌控制檯中創建項目。我已經下載P12密鑰文件,然後寫示例代碼插入文件來測試它:如何從項目中瀏覽文件谷歌驅動器
<?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/contrib/Google_Oauth2Service.php";
$scopes = array('https://www.googleapis.com/auth/drive');
$accountEmail = 'SECRET';
$accountP12FilePath = './key.p12';
$key = file_get_contents($accountP12FilePath);
$auth = new Google_AssertionCredentials($accountEmail, $scopes, $key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
$service = new Google_DriveService($client);
$file = new Google_DriveFile();
$file->setTitle('Test Title');
$file->setDescription('test description');
$parent = new Google_ParentReference();
$file->setParents(array($parent));
$createdFile = $service->files->insert($file, array(
'data' => 'test data',
'mimeType' => 'text/plain'
));
var_dump($createdFile);
?>
它轉儲Google_DriveFile對象有許多的URL。其中一些,如'downloadUrl',返回401未授權錯誤。當我嘗試'alternateLink'時,它顯示帶有我需要訪問的信息的Google驅動器頁面,以及請求訪問和切換帳戶的按鈕。
它還表示我當前登錄了我的帳戶,這是用來創建項目的工具,並且可以在Google控制檯頁面的項目的權限頁面上看到。
如何訪問屬於我在腳本中使用的項目的驅動器?