Google Drive Service Accounts Additional Storage

后端 未结 3 1097
天涯浪人
天涯浪人 2020-12-12 05:17

I have an app where the users of my app can upload/download files, I don\'t want them to authorize every time they want to upload a file. So I came to know that Service Acco

3条回答
  •  星月不相逢
    2020-12-12 05:36

    A solution to store files in your own google drive account rather than google drive of service account is to grant owner permission to your service account in your console of google developer.

    And then create a folder inside your personal google drive and get the id for that folder.

    and when you upload file using service account make sure to put id of that folder as parent id for file which you are uploading like this

        $file = new Google_Service_Drive_DriveFile();
          $file->title = "Automata";
        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId('Your Folder id');
        $file->setParents(array($parent));
          $requestss = $drive_service->files->insert($file,array(
              'data' => $data,
              'mimeType' => 'application/pdf',
            ));
    
        $permission = new Google_Service_Drive_Permission();
        $permission->setRole( 'writer' );
        $permission->setType( 'anyone' );
        $permission->setValue( 'youraccount@gmail.com' );
        $drive_service->permissions->insert( $requestss->getId(), $permission ); 
    

    Now you will be able to upload files to your own google drive instead of service account drive

    Hope this helps

提交回复
热议问题