How to use Google Drive in Laravel

Install package Google Drive API V3

composer require nao-pon/flysystem-google-drive:~1.1

Thêm provider GoogleDriveServiceProvider vào file config/app.php

'providers' => [
    // ...
    App\Providers\GoogleDriveServiceProvider::class,
    // ...
],

Nội dung provider tham khảo ở dưới

https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/app/Providers/GoogleDriveServiceProvider.php

Update tiếp config/filesystems.php

'disks' => [
    // ...
    'google' => [
        'driver' => 'google',
        'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
    ],
    // ...
],

Update .env cho các values trêntrên

FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=
GOOGLE_DRIVE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=
GOOGLE_APP_ID=
GOOGLE_DRIVE_REFRESH_TOKEN=
GOOGLE_DRIVE_FOLDER_ID=null

Đến đây tạm hiểu vấn đề như sau

Ta tạo thêm 1 extend cho Filesystem. Như vậy sau này có thể dùng Storage::disk(‘google’), hoặc dùng Storage::cloud – Vì FILESYSTEM_CLOUD đã set google rồi.

Vấn đề tiếp theo là lấy refresh token

https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/2-getting-your-refresh-token.md

Bổ sung

Lấy Google Client ID & Secret https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/1-getting-your-dlient-id-and-secret.md

Để dùng cách trên lấy refresh token thì set callback URL là https://developers.google.com/oauthplayground

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: