使用 google drive API PHP V3 权限错误删除文件

乌萨马卡迈勒

我在使用 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"
     }
    }
Tanaike

Google_Service_Drive::DRIVE_METADATA_READONLY不能用于使用 Drive API 删除文件。那么如何使用Google_Service_Drive::DRIVE作为范围呢?

当您修改范围时,请删除drive-php.jsonat文件/root/.credentials/,然后再次运行脚本。通过这种方式,可以检索反映修改范围的访问令牌和刷新令牌。

然后,请确认是否再次启用 Drive API。

如果这对你没有用,我很抱歉。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Google Drive Api v3创建文件(javascript)

如何使用Google Drive Java API v3在Google Drive上复制文件?

如何使用带有Google Drive API v3 Java的服务帐户访问Team Drive

如何使用Java中的Google Drive API v3获取上载文件到Google Drive的可共享链接

如何在Google Drive API v3 PHP中的特定文件夹中列出文件

Google Drive PHP v3 API文件元数据

无法使用Drive API v3更新Google Drive文件-资源正文包含不可直接写的字段

使用Google Drive API v3获取Google文档的文件内容

使用Google Drive API v3移动文件

如何使用带有Google Drive .NET API v3的服务帐户访问Team Drive

使用Google Drive API v3将PDF文件转换为Google文档

Google Drive API v3更改文件权限并获取公开共享的链接(Python)

使用Google Drive API V3与他人共享文件时如何设置文件功能?

如何使用Google Drive Api v3和php显示文本文件的内容?

如何使用Python和Drive API v3下载Google云端硬盘文件

限制使用Google Drive API V3下载文件

如何使用NodeJS和Google Drive API v3将文件移至垃圾箱

使用python,我无法从Google Drive API v3访问共享驱动器文件夹

有什么方法可以使用适用于PHP的Google Drive API v3来删除文件吗?

如何从 google drive api v3 获取文件列表?

如何使用 Google Drive API V3 将文件上传到 Group Drive(共享驱动器)?

如何使用 Python 和 Drive API v3 从 Google Drive 下载文件

搜索文件 Google Drive API v3

使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置?

Google Drive Api V3 文件复制不起作用 - PHP

在 Google Drive API V3 中使用 update() 和 addParents 移动文件不会永久生效

如何从 Google Drive 上传和下载文件(使用 Rest Api v3)

使用 Google Drive API v3 將 .docx 或 .doc 文件轉換為 google docs 文件

如何使用 Google Drive API v3 将可恢复上传到 Python 中的共享文件夹?