recently I have set up an automatic file creation system on the google drive, I have integrated the API into my symfony application, I manage to authenticate myself, etc., but I do not block on the step of creation of the file.
I have this error that occurs:
{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission: Request had insufficient authentication scopes." } ], "code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes." } }
I do not really understand where this can come from knowing that I have set the scope indicated in the documentation: $client->addScope(Google_Service_Drive::DRIVE);
my function :
public function createFolder($name) { $client = $this->getClient(); // $client->setAuthConfig('code_secret_client.json'); $client->addScope(Google_Service_Drive::DRIVE); $client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ'); $service = new Google_Service_Drive($client); $folder = new Google_Service_Drive_DriveFile(); $folder->setName($name); $folder->setMimeType('application/vnd.google-apps.folder'); $result = $service->files->create($folder); dump($result); return $result; }
Do you have any ideas ?
Advertisement
Answer
I was able to reproduce via google example
<?php /** * Copyright 2018 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // [START drive_quickstart] require __DIR__ . '/vendor/autoload.php'; if (php_sapi_name() != 'cli') { throw new Exception('This application must be run on the command line.'); } /** * Returns an authorized API client. * @return Google_Client the authorized client object */ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Drive API PHP Quickstart'); $client->setScopes(Google_Service_Drive::DRIVE); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); // Load previously authorized token from a file, if it exists. // The file token.json stores the user's access and refresh tokens, and is // created automatically when the authorization flow completes for the first // time. $tokenPath = 'token.json'; if (file_exists($tokenPath)) { $accessToken = json_decode(file_get_contents($tokenPath), true); $client->setAccessToken($accessToken); } // If there is no previous token or it's expired. if ($client->isAccessTokenExpired()) { // Refresh the token if possible, else fetch a new one. if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:n%sn", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); $client->setAccessToken($accessToken); // Check to see if there was an error. if (array_key_exists('error', $accessToken)) { throw new Exception(join(', ', $accessToken)); } } // Save the token to a file. if (!file_exists(dirname($tokenPath))) { mkdir(dirname($tokenPath), 0700, true); } file_put_contents($tokenPath, json_encode($client->getAccessToken())); } return $client; } $name = "Stackoverflow"; // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Drive($client); $folder = new Google_Service_Drive_DriveFile(); $folder->setName($name); $folder->setMimeType('application/vnd.google-apps.folder'); $result = $service->files->create($folder); // [END drive_quickstart]
Same error. Then I checked token.json, at “scope” it had readonly. I had deleted that token and requested again Make sure it has “See, edit, create, and delete all of your Google Drive files” option to select when giving perissions, and it will work.
this line was problem on createFolder.
$client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ');