项目作者: vatri

项目描述 :
Google Drive API Bundle for Symfony 4
高级语言: PHP
项目地址: git://github.com/vatri/Google-Drive-Bundle.git
创建时间: 2019-06-07T13:12:03Z
项目社区:https://github.com/vatri/Google-Drive-Bundle

开源协议:

下载


Google-Drive-Bundle

Google Drive API Bundle for Symfony 6 and 7.

Features

  • 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)

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

  1. $ composer require vatri/google-drive-bundle

Applications that don’t use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

  1. $ 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.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:

  1. // config/bundles.php
  2. return [
  3. // ...
  4. App\Vatri\GoogleDriveBundle\VatriGoogleDriveBundle::class => ['dev' => true, 'test' => true],
  5. ];

Usage

1. Add/edit following variables in your .env file:

  1. VATRI_GOOGLE_DRIVE_CREDENTIALS_FILE=config/YOUR_FILENAME.json`
  2. VATRI_GOOGLE_DRIVE_REDIRECT_AFTER_AUTH=/

Don’t forget to replace default values with yours.

2. Create (or check if exists) /config/routes/vatri_google_drive.yaml file with following contents:

  1. vatri_google_drive:
  2. resource: '@VatriGoogleDriveBundle/Controller/'
  3. type: annotation

3. Download and configure JSON credentials file

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

4. Check and use AuthController

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.

  1. Run

php bin/console debug:router

and check if you have a route like this:

  1. vatri_google_drive_auth ANY ANY ANY <href=>/vatri_google_drive/auth
  1. Now in order to authenticate users, you need to add link to the route like this:
  1. <a href="{{ path('vatri_google_drive_auth') }}">
  2. Login
  3. </a>

5. Use DriveApiService in your controller or another Symfony part like this:

  1. use Vatri\GoogleDriveBundle\Service\DriveApiService;
  2. ...
  3. public function test(DriveApiService $driveApiService): Response
  4. {
  5. if($driveApiService->isTokenExpired()){
  6. return $this->redirectToRoute( $driveApiService->getAuthRouteName() );
  7. }
  8. $folderId = '[YOUR ID]';
  9. $res = $driveApiService->listFiles($folderId, false, true );
  10. dd($res);
  11. }

6. Check if Drive API access token is expired and authorize if required:

Add the following code to your controller or other part:

  1. if($driveApiService->isTokenExpired()){
  2. // When auth is finished, redirect back to this URL:
  3. $driveApiService->setRedirectPathAfterAuth(
  4. $this->get('request_stack')->getCurrentRequest()->getRequestUri()
  5. );
  6. // Redirect
  7. return $this->redirectToRoute( $driveApiService->getAuthRouteName() );
  8. }

Roadmap

Version 1.2

  • Symfony Flex recipe
  • Logout controller
  • Configure token storage (including custom one)

Version 1.1

  • renameFolder() method

Version 1.0

  • Automatically refresh access_token using refresh_token
  • Uniformed responses from DriveApiService (a class)