0
votes

I'm pretty new to the Google APIs in general, so please bear with me.

I have a PHP script with which I can successfully create a Google Sheet and manipulate it via the Google Sheets PHP API.

Once I create a new Google Sheet I want to move it to a folder which has the permission level I want the Sheet to have (all users within my domain with the link can read/write the sheet).

From looking around I gather that the way to do this is through the Google Drive API, not directly through Google Sheets.

My problem is that when I try to move the Sheet I just created into a folder I created in Google Drive, I get an "Insufficient Permission" error.

I've double checked that the Drive API is enabled in the Google API console. I did not set up Drive UI Integration because that didn't seem relevant to a script running from the command line.

Here's the code I'm using to try to move the sheet to a new folder:

define('SCOPES', implode(' ', array( \Google_Service_Sheets::SPREADSHEETS)));
...
$this->client = new \Google_Client();
$this->client->setApplicationName("My Application Name");
$this->client->setScopes(SCOPES);
$this->client->setAuthConfig("/full/path/to/my/client_secret.json");
...
$this->client->setAccessType('offline');
$driveService = new \Google_Service_Drive($this->client);
$emptyFileMetadata = new \Google_Service_Drive_DriveFile();
$file = $driveService->files->get(
    $this->getGoogleSheetID(),
    array('fields' => 'parents')
);
$previousParents = join(',', $file->parents);
$file = $driveService->files->update(
    "{ID of Google Sheet I just created}",
    $emptyFileMetadata,
    array(
        'addParents' => "{id of folder I created manually in Google Drive}",
        'removeParents' => $previousParents,
        'fields' => 'id, parents'
    )
);

When this code runs I see an exception which includes:

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{ 
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "insufficientPermissions",
                "message": "Insufficient Permission"
            }
        ],
        "code": 403,
        "message": "Insufficient Permission"
    }
}

Any ideas?

1
Well... What permissions do your account have, and the Google Sheet, and the folder?tehhowch
I created both so I'm assuming I have write permissions on both.David Ranney
When I view details on the folder it says, correctly, "Anyone at {my domain} with the link can edit."David Ranney

1 Answers

1
votes

As mentioned in the guide, using Files.update to move files between folders require at least one of these authorization scopes:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.scripts
https://www.googleapis.com/auth/drive.metadata