To authenticate with Google Cloud Storage using OAuth in Laravel, you can follow these steps:
1. Create a Google Cloud Platform Project:
- Go to the Google Cloud Console (https://console.cloud.google.com/) and create a new project.
2. Enable the Google Cloud Storage API:
- In the Google Cloud Console, navigate to the "APIs & Services" section and enable the "Google Cloud Storage JSON API" for your project.
3. Create OAuth Credentials:
- In the "APIs & Services" section, go to the "Credentials" tab and click on "Create Credentials".
- Select "OAuth client ID" as the credential type.
- Choose the application type as "Web application".
- Add the authorized redirect URIs, which will be in the format `https://your-app.test/login/google/callback` (replace `your-app.test` with your actual domain).
- Click "Create" to generate the OAuth client ID and client secret.
4. Configure the Credentials in Laravel:
- In your Laravel application's `.env` file, add the following environment variables:
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_REDIRECT=https://your-app.test/login/google/callback
- In your `config/services.php` file, add the following configuration:
php
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
],
5. Install the Google Cloud Storage Package:
- Install the `spatie/laravel-google-cloud-storage` package using Composer:
composer require spatie/laravel-google-cloud-storage
6. Configure the Google Cloud Storage Disk:
- In your `config/filesystems.php` file, add the following configuration for the Google Cloud Storage disk:
php
'gcs' => [
'driver' => 'gcs',
'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', base_path('service-account.json')),
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''),
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null),
'api_endpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT', null),
'visibility' => 'public',
'visibility_handler' => null,
'metadata' => ['cacheControl' => 'public,max-age=86400'],
],
7. Add the Required Environment Variables:
- In your `.env` file, add the following environment variables:
GOOGLE_CLOUD_KEY_FILE=service-account.json
GOOGLE_CLOUD_PROJECT_ID=your-project-id
GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket
- Replace the placeholders with your actual Google Cloud project ID and storage bucket name.
By following these steps, you can authenticate with Google Cloud Storage using OAuth in your Laravel application. The `spatie/laravel-google-cloud-storage` package will handle the authentication and interaction with Google Cloud Storage on your behalf.
Remember to configure the necessary permissions and access controls in your Google Cloud Storage bucket to allow your Laravel application to perform the desired operations.
Citations:[1] https://fajarwz.com/blog/create-login-with-google-oauth-using-laravel-socialite/
[2] https://www.iankumu.com/blog/laravel-google-cloud-storage/
[3] https://github.com/spatie/laravel-google-cloud-storage
[4] https://packagist.org/packages/spatie/laravel-google-cloud-storage
[5] https://www.twilio.com/en-us/blog/file-upload-with-laravel-and-google-cloud-storage