myLogger = \Config\Services::mylogger(); $this->client = new Google_Client(); $this->client->setAuthConfig(ROOTPATH . 'nhance-app-google-drive.json'); // App credentials // putenv('GOOGLE_APPLICATION_CREDENTIALS=' . ROOTPATH . 'nhance-app-google-drive.json'); $this->client->useApplicationDefaultCredentials(); $this->client->addScope(Google_Service_Drive::DRIVE); // Full access to Google Drive } public function getDriveService() { return new Google_Service_Drive($this->client); } // this function serve no purpose on anywhere, just for test purpose public function listFiles(string $searchQuery = '') { $service = $this->getDriveService(); // $searchQuery = "name contains 'test' and 'vitvelz@gmail.com' in readers"; $searchQuery = "name contains 'test' or name contains 'new'"; // $searchQuery = "name contains 'test' and mimeType = 'application/vnd.google-apps.folder' or name contains 'new' and mimeType = 'application/vnd.ms-excel'"; // List the files $files = $service->files->listFiles([ 'q' => $searchQuery, // 'supportsAllDrives' => true, // 'includeItemsFromAllDrives' => true, 'fields' => 'nextPageToken, files(id, name, mimeType, size, createdTime, modifiedTime, owners, shared, permissions, webViewLink, thumbnailLink)', 'pageSize' => 20, ]); // print_r($files); echo "\n**************\n"; foreach ($files->getFiles() as $file) { echo 'File ID: ' . $file->getID() . "\n"; echo 'File Name: ' . $file->getName() . "\n"; // echo 'MIME Type: ' . $file->getMimeType() . "\n"; // echo 'Size: ' . $file->getSize() . " bytes\n"; // echo 'Created Time: ' . $file->getCreatedTime() . "\n"; // echo 'Modified Time: ' . $file->getModifiedTime() . "\n"; // echo 'Owner: ' . $file->getOwners()[0]->getEmailAddress() . "\n"; // echo 'Shared: ' . ($file->getShared() ? 'Yes' : 'No') . "\n"; // echo 'Permissions: ' . json_encode($file->getPermissions()) . "\n"; // echo 'Web View Link: ' . $file->getWebViewLink() . "\n"; // echo 'Thumbnail Link: ' . $file->getThumbnailLink() . "\n"; echo 'parents: ' .$file->getParents()."\n"; echo "-----\n"; } } // this fucntion is not generating any new token, just for testing purpose public function generateNewToken() { // $this->uploadFile();die(); $this->listFiles();die(); // dd('called'); if (!is_cli()) { throw new \RuntimeException('This method can only be accessed via the command line.'); } if (php_sapi_name() != 'cli') { throw new Exception('This application must be run on the command line.'); } $authUrl = $this->client->createAuthUrl(); CLI::write('Open the following link in your browser'); CLI::write("\n"); CLI::write($authUrl); // printf ("Open the following link in your browser:\n%s\n", $authUrl); CLI::write('Enter verification code: '); // print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); $accessToken = $this->client->fetchAccessTokenWithAuthCode($authCode); if (array_key_exists('error', $accessToken)) { $this->myLogger->logme('error', 'Error in getting access token from authCode' . join(', ', $accessToken)); throw new \Exception(join(', ', $accessToken)); } $this->storeToken($accessToken); $this->myLogger->logme('error', 'New token generated and stored in the writepath for google drive.'); print 'New token generated and stored in the writepath for google drive.'; } /** * Upload a file to Google Drive * this function not used in anywhere */ public function uploadFile($filePath = '', $fileName= '') { $service = $this->getDriveService(); // $file = new \Google_Service_Drive_DriveFile(); // $file->setName('tata.xlsx'); // $file->setParents('TEST'); // Create a new file metadata object $file = new \Google_Service_Drive_DriveFile([ 'name' => 'mypdf.pdf', 'parents' => ['1M0GH3GbNUOYZLNeHXA5U7C_P2NSx7F7O'] // Specify the folder ID here ]); $data = file_get_contents('C:\Users\Venba\Desktop\nhance_bpf.pdf'); $createdFile = $service->files->create($file, [ 'data' => $data, 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart', 'fields' => 'id' // Specify fields to return ]); echo $createdFile->id; } }