简体   繁体   中英

Google Drive PHP API returns deleted files

I'm trying to sync files between Amazon S3 and Google Drive with a service.

I created a service account and shared a folder with its email. Then with the automated service I built, I created some folders.

Everything was fine until I tried to delete these folders from Google Drive UI. They disappeared from the UI, but my service still receives them as if they were present. Here is the code I'm using to list the files/folders:

private function retrieveAllGoogleDriveFiles($service) {
    $result = array();
    $pageToken = NULL;
    do {
        try {
            $parameters = array();
            if ($pageToken) {
                $parameters['pageToken'] = $pageToken;
            }
            $parameters['q'] = "trashed=false";
            $files = $service->files->listFiles($parameters);

            $result = array_merge($result, $files->getItems());
            $pageToken = $files->getNextPageToken();
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
            $pageToken = NULL;
        }
    } while ($pageToken);
    return $result;
}

Here is the authorization mechanism I'm using:

        $credentials = new Google_Auth_AssertionCredentials(
            '1234567890-somehashvalueshere123123@developer.gserviceaccount.com',
            array('https://www.googleapis.com/auth/drive'),
            $private_key,
            'notasecret',                                
            'http://oauth.net/grant_type/jwt/1.0/bearer'
        );

        $googleClient = new Google_Client();
        $googleClient->setAssertionCredentials($credentials);
        if ($googleClient->getAuth()->isAccessTokenExpired()) {
            $googleClient->getAuth()->refreshTokenWithAssertion();
        }

        $googleService = new Google_Service_Drive($googleClient);
        $files_list = $this->retrieveAllGoogleDriveFiles($googleService);

The case where a user deletes a file/folder from Google Drive is real to me and I should make the sync back to S3 and remove the file/folder from there also.

Thanks in advance.

When a folder is shared among multiple users, it is the owner who has the ability to delete a folder, not the other users.

Essentially what is happening is by hitting 'remove', the user is removing it from their Drive (unsubscribing), they are not deleting the folder. This is why it appears still appears in your service accounts file list.

One possible solution solution is to do a change owner so the non-service account owns the file. In that case, remove really will be a trash/delete sort of operation. However, without understanding your use case/underlying problem to solve, it is hard to through out solutions. Some of these things may be easier if the users are enterprise/work accounts rather than private/personal accounts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM