问题描述
我在使用Google驱动器PHP API V3时遇到问题,我正在尝试使用以下代码从驱动器中删除文件:
I have an issue with google drive PHP API V3 I'm trying to remove a file from the drive using the code below:
这是我正在使用的代码:
This is the code I'm using:
<?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,但是当我尝试删除它时,出现以下错误.知道为什么吗?
All is working I can get the ID of the file but when I try to delete it I get the below error.Any idea why please?
谢谢
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
作为范围呢?
Google_Service_Drive::DRIVE_METADATA_READONLY
cannot be used for deleting files using Drive API. So how about using Google_Service_Drive::DRIVE
as the scope?
修改范围时,请删除/root/.credentials/
中的drive-php.json
文件,然后再次运行脚本.这样,就可以检索反映修改后的作用域的访问令牌和刷新令牌.
When you modified the scope, please remove the file of drive-php.json
at /root/.credentials/
, and run the script again. By this, the access token and refresh token reflected the modified scope can be retrieved.
然后,请确认是否再次启用Drive API.
And then, please confirm whether Drive API is enabled again.
如果这对您没有用,很抱歉.
If this was not useful for you, I'm sorry.
这篇关于使用Google Drive API PHP V3权限删除文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!