我已经关注https://developers.google.com/drive/api/v2/reference/files/list,成功!我都知道了但不是我的“驱动器”。我收到警告:“您需要在此处被授予访问权限。那么该如何解决以上错误?
我创建了一个新项目,服务帐户密钥和OAuth 2.0客户端ID。但这很糟糕,它无法正确链接到我的“ Google云端硬盘”帐户
这是我的完整代码
<?php
session_start ();
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';
require_once 'vendor/autoload.php';
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = 'xxx@phim-240702.iam.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'phim-240702-a98343eb742a.p12';
function buildService(){
global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$client = new Google_Client();
$client->setApplicationName("googletest5");
$client->setDeveloperKey("b2016fa55e916faf35337ccb1db830ecdb590cc3");
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
/**
* Insert new file.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $title Title of the file to insert, including the extension.
* @param string $description Description of the file to insert.
* @param string $parentId Parent folder's ID.
* @param string $mimeType MIME type of the file to insert.
* @param string $filename Filename of the file to insert.
* @return Google_Service_Drive_DriveFile The file that was inserted. NULL is
* returned if an API error occurred.
*/
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => '$mimeType',
));
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
/**
* Retrieve a list of File resources.
*
* @param Google_Service_Drive $service Drive API service instance.
* @return Array List of Google_Service_Drive_DriveFile resources.
*/
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array(
'maxResults' => '100'
);
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;
try {
$root_id = null;
$service = buildService();
print_r(retrieveAllFiles($service));
}catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
?>
答案 0 :(得分:0)
您需要记住的是,服务帐户不是您。服务帐户是虚拟用户。它有自己的Google云端硬盘帐户。当您将文件上传到服务帐户驱动器帐户时,只有该帐户才可以访问,因为它是该帐户的所有者。
如果您希望它能够访问您的个人驱动器帐户上的文件,则需要通过与服务帐户的电子邮件地址共享文件来授予其访问权限。这是预先批准服务帐户的方式。
对于获取文件的路径,没有简单的方法可以做到,您需要逐步获取文件,然后获取其父文件,然后再获取更多的父文件,直到达到最高点为止。以这种方式建立它。