我在使用 google drive PHP API V3 时遇到问题,我正在尝试使用以下代码从驱动器中删除文件:
这是我正在使用的代码:
<?php
require_once __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Google Drive API PHP');
define('CREDENTIALS_PATH', '/root/.credentials/drive-php.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_METADATA_READONLY
)
));
if (php_sapi_name() != 'cgi-fcgi') {
throw new Exception('This application must be run on the command line.');
}
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$accessToken = json_decode(file_get_contents(CREDENTIALS_PATH), true);
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents(CREDENTIALS_PATH, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$service = new Google_Service_Drive($client);
$optParams = array(
'fields' => 'files(id, createdTime)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) != 0) {
foreach ($results->getFiles() as $file) {
$service->files->delete($file['id']);
}
}
一切正常,我可以获得文件的 ID,但是当我尝试删除它时,出现以下错误。知道为什么吗?
谢谢
PHP Fatal error: Uncaught Google_Service_Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
Google_Service_Drive::DRIVE_METADATA_READONLY
不能用于使用 Drive API 删除文件。那么如何使用Google_Service_Drive::DRIVE
作为范围呢?
当您修改范围时,请删除drive-php.json
at文件/root/.credentials/
,然后再次运行脚本。通过这种方式,可以检索反映修改范围的访问令牌和刷新令牌。
然后,请确认是否再次启用 Drive API。
如果这对你没有用,我很抱歉。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句