vatri/google-drive-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vatri/google-drive-bundle

Composer 安装命令:

composer require vatri/google-drive-bundle

包简介

Google Drive API Bundle

README 文档

README

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:

$ 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:

$ 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:

// config/bundles.php

return [
    // ...
    App\Vatri\GoogleDriveBundle\VatriGoogleDriveBundle::class => ['dev' => true, 'test' => true],
];

Usage

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

VATRI_GOOGLE_DRIVE_CREDENTIALS_FILE=config/YOUR_FILENAME.json`
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:

vatri_google_drive:
    resource: '@VatriGoogleDriveBundle/Controller/'
    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:

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:
<a href="{{ path('vatri_google_drive_auth') }}">
    Login
</a>

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

 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);
 }

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

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() );
}

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)

统计信息

  • 总下载量: 6.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固