如何删除文件夹驱动器api

时间:2016-11-02 09:52:22

标签: php permissions google-drive-api google-api-php-client

即使我尝试更改权限,我也无法删除文件夹(由其他人创建),我有一个控制台应用程序,当前经过身份验证的用户可以执行以下操作:

  1. 创建文件夹/文件
  2. 移动文件
  3. 的范围:

    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.appdata',
    'https://spreadsheets.google.com/feeds',
    

    尝试:

    1.设置当前用户的权限

    public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
    {
        $permission = new \Google_Service_Drive_Permission();
        $permission->setRole( 'owner' );
        $permission->setType( 'user' );
        $permission->setEmailAddress('someId@developer.gserviceaccount.com');
        $permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
        $this->googleDriveClient->files->delete($folder->getId());
    }
    

    结果:

      

    [Google_Service_Exception] {
      “错误”:{
          “错误”:[
           {
            “域名”:“全球”,
            “理由”:“internalError”,
            “消息”:“内部错误”
           }
          ],
          “代码”:500,
          “消息”:“内部错误”}}

    2.将权限类型设置为任何人

    public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
    {
        $permission = new \Google_Service_Drive_Permission();
        $permission->setRole( 'owner' );
        $permission->setType( 'anyone' );
        $permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
        $this->googleDriveClient->files->delete($folder->getId());
    }
    

    结果:

      

    [Google_Service_Exception]
      {
      “错误”:{

    "errors": [                                                                
     {                                                                         
      "domain": "global",                                                      
      "reason": "insufficientFilePermissions",                                 
      "message": "The user does not have sufficient permissions for this file  ."                                                         
    
     }                                                                         
    ],                                                                         
    "code": 403,                                                               
    "message": "The user does not have sufficient permissions for this file."      }                                                         
    
         

    }

    尝试3:冒充创作者

    $this->googleClient->setAccessType('offline');
    $this->googleClient->setSubject('x@domain.com');//if removed everything works 
    
      

    [GuzzleHttp \异常\ ClientException]
      客户端错误:POST https://www.googleapis.com/oauth2/v4/token   导致401 Unauthorized回复:
      {
      “错误”:“unauthorized_client”,
      “error_description”:“请求中未经授权的客户端或范围。”,
      “error_uri”:“”   }

    我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

只有文件夹的所有者才能删除该文件夹。 使用服务帐户Using OAuth 2.0 for Server to Server Applications,获取文件夹Perform Google Apps Domain-Wide Delegation of Authority的所有者的身份。

一旦被授权为该文件夹的所有者,就应该可以删除该文件夹。