调用DELETE时出错 - (403)此文件的权限不足“| Google Drive API

时间:2018-01-17 12:46:16

标签: php rest google-api google-drive-api drive

我创建了以下代码。我必须从我的Google云端硬盘文件夹下载文件。该文件夹是共享的。下载后,我必须从Google云端硬盘中删除这些文件。

我试过了 -

  1. $file = $service->parents->delete($file_id,$folder_id); - 它没有做任何事情,也没有任何错误。
  2. $file = $service->files->trash($file_id); - 它提供错误调用DELETE - (403)此文件的权限不足“错误。
  3. 我的代码:

    <?php
    require_once "google/google-api-php-client/src/Google_Client.php";
    
    require_once "google/google-api-php-client/src/contrib/Google_DriveService.php";
    
    require_once "google/google-api-php-client/src/contrib/Google_Oauth2Service.php";
    
    require_once "google/vendor/autoload.php";
    
    $file_id = '1bJm_cqIRVh5RaVrqVXGRL0CSYwTBlZur';
    $folder_id='1gEllj4B9TCnLPe_dnl1ujX4u8smLL-Ky';
    
    $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
    $SERVICE_ACCOUNT_EMAIL = 'service_account_email@domain.com';
    $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'GoogleDriveApi-7cd8056e9eae.p12';
    
    
    function buildService() {//function for first build up service
    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->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    return new Google_DriveService($client);
    

    }

    function printFilesInFolder($service, $folderId) {
      $pageToken = NULL;
      $arrayFile=array();
      do {
        try {
          $parameters = array();
          if ($pageToken) {
            $parameters['pageToken'] = $pageToken;
          }
          $children = $service->children->listChildren($folderId, $parameters);
          $arrayFile=$children;
          $pageToken = $children->getNextPageToken();
        } catch (Exception $e) {
          print "An error occurred: " . $e->getMessage();
          $pageToken = NULL;
        }
      } while ($pageToken);
      return $arrayFile;
    }
    
    try {
    
        $service = buildService();
    
        $children=printFilesInFolder($service,$folder_id);
    
        $myfile = fopen("D:/list.txt", "wb") or die("Unable to open file!");
        foreach ($children->getItems() as $child) {
            print "\r\nFile Id: " . $child->getId();
            fwrite($myfile, $child->getId());
            fwrite($myfile, "\r\n");  
          }      
        $file = $service->parents->delete($file_id,$folder_id);
        $file = $service->files->trash($file_id);
    
        fclose($myfile);
    
    } catch (Exception $e) {
        print "An error occurred1: " . $e->getMessage();
    }
    ?>
    

1 个答案:

答案 0 :(得分:0)

您的error表示用户没有对该文件的写入权限,并且某个应用正在尝试修改该特定文件。

  

建议采取的行动:向用户报告需要提出要求   那些权限,以便更新文件。你可能也想   检查检索到的元数据中的用户访问级别   files.get并使用它将您的用户界面更改为只读用户界面。