Google Drive API Bundle for Symfony 6 and 7.
- Authorize via Google API
- Manage Google Drive files and folders
- Create a folder (recursively)
- Check if a folder exists
- Find a file by ID
- Delete a file
- Download file
- List files
- Copy a file to specific directory
- Upload a file
- Add "starred" flag to a file/folder
- Rename resource (file or folder)
Open a command console, enter your project directory and execute:
$ composer require vatri/google-drive-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require vatri/google-drive-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
App\Vatri\GoogleDriveBundle\VatriGoogleDriveBundle::class => ['dev' => true, 'test' => true],
];
VATRI_GOOGLE_DRIVE_CREDENTIALS_FILE=config/YOUR_FILENAME.json`
VATRI_GOOGLE_DRIVE_REDIRECT_AFTER_AUTH=/
Don't forget to replace default values with yours.
vatri_google_drive:
resource: '@VatriGoogleDriveBundle/Controller/'
type: annotation
Download your JSON credentials file from Google Console to /config folder within Symfony project.
Edit following variables in .env file:
VATRI_GOOGLE_DRIVE_CREDENTIALS_FILE=config/google-drive-api-client_secrets.json-example.json
VATRI_GOOGLE_DRIVE_REDIRECT_AFTER_AUTH=/path/to/your/route
AuthController is default controller required for authorization of users via Google API. By default it saves an access token to cookie.
Also note that if you use this controller for authorization, you will add below route as callback URL in Google Console Credentials configuration.
- Run
php bin/console debug:router
and check if you have a route like this:
vatri_google_drive_auth ANY ANY ANY <href=>/vatri_google_drive/auth
- Now in order to authenticate users, you need to add link to the route like this:
<a href="{{ path('vatri_google_drive_auth') }}">
Login
</a>
use Vatri\GoogleDriveBundle\Service\DriveApiService;
...
public function test(DriveApiService $driveApiService): Response
{
if($driveApiService->isTokenExpired()){
return $this->redirectToRoute( $driveApiService->getAuthRouteName() );
}
$folderId = '[YOUR ID]';
$res = $driveApiService->listFiles($folderId, false, true );
dd($res);
}
Add the following code to your controller or other part:
if($driveApiService->isTokenExpired()){
// When auth is finished, redirect back to this URL:
$driveApiService->setRedirectPathAfterAuth(
$this->get('request_stack')->getCurrentRequest()->getRequestUri()
);
// Redirect
return $this->redirectToRoute( $driveApiService->getAuthRouteName() );
}
- Symfony Flex recipe
- Logout controller
- Configure token storage (including custom one)
- renameFolder() method
- Automatically refresh access_token using refresh_token
- Uniformed responses from DriveApiService (a class)