😆Setup a Laravel Storage driver with Google Drive API (ok)

https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011#setup-a-laravel-storage-driver-with-google-drive-api

Souce tham khảo. https://khamphukhoacaugiay.vn/arrow-up-right

file-download
5KB

Source code

file-archive
2MB
archive

Đã thực thiện thành công

C:\xampp82\htdocs\lva4\database\migrations\2014_10_12_000000_create_users_table.php

C:\xampp82\htdocs\lva4\app\Models\User.php

C:\xampp82\htdocs\lva4\routes\web.php

C:\xampp82\htdocs\lva4\app\Http\Controllers\GoogleDriveController.php

Cấu hình lấy ở đây https://console.cloud.google.com/apis/credentials/oauthclient/710157078117-9t8ppso74lgg3f90nrf24nk8tp8t4ula.apps.googleusercontent.com?project=test-gd-apiarrow-up-right

https://developers.google.com/oauthplaygroundarrow-up-right

Kết quả:

Log in to your Google Account and go to this website:

https://console.developers.google.com/arrow-up-right

Create a new project using the dropdown at the top.

Create a new projectarrow-up-right

After you enter a name, it takes a few seconds before the project is successfully created on the server.

Make sure you have the project selected at the top.

Then go to Library and click on "Drive API" under "Google Apps APIs".

Find Google Drive APIarrow-up-right

And then Enable it.

Enable Google Drive APIarrow-up-right

Then, go to "Credentials" and click on the tab "OAuth Consent Screen". Fill in a "Product name shown to users" and Save it. Don't worry about the other fields.

Consent Screenarrow-up-right

Then go back to Credentials, click the button that says "Create Credentials" and select "OAuth Client ID".

Create Credentialsarrow-up-right

Choose "Web Application" and give it a name.

Enter your "Authorized redirect URIs", preferably your test URL (http://mysite.devarrow-up-right) and your production URL (https://mysite.comarrow-up-right) - or create a separate production key later. Also add https://developers.google.com/oauthplaygroundarrow-up-right temporarily, because you will need to use that in the next step.

Credentialsarrow-up-right

Click Create and take note of your Client ID and Client Secret.

Now head to https://developers.google.com/oauthplaygroundarrow-up-right.

Make sure you added this URL to your Authorized redirect URIs in the previous step.

In the top right corner, click the settings icon, check "Use your own OAuth credentials" and paste your Client ID and Client Secret.

Use your own OAuth credentialsarrow-up-right

In step 1 on the left, scroll to "Drive API v3", expand it and check each of the scopes.

Check Scopesarrow-up-right

Click "Authorize APIs" and allow access to your account when prompted.

When you get to step 2, check "Auto-refresh the token before it expires" and click "Exchange authorization code for tokens".

Exchange authorization code for tokensarrow-up-right

When you get to step 3, click on step 2 again and you should see your refresh token.

Refresh Tokenarrow-up-right

If you want to store files in your Google Drive root directory, then the folder ID can be null. Else go into your Drive and create a folder.

Because Google Drive allows for duplicate names, it identifies each file and folder with a unique ID. If you open your folder, you will see the Folder ID in the URL.

Folder IDarrow-up-right

Pull in Flysystem Adapter for Google Drivearrow-up-right:

Add the storage disk configuration to config/filesystem.php:

And update your .env file:

Save GoogleDriveServiceProvider.php to app/Providers and add it to the providers array in config/app.php:

Now you should be up and running:

Last updated