Use Google Drive API with Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering External APIs: Using the Google Drive API with Laravel

Building robust applications often requires integrating external services. When you want to leverage the power of the Google Drive API within a Laravel project to manage files like PDFs, the initial hurdle is usually translating raw PHP examples into a structured, maintainable framework that aligns with Laravel's conventions. Many developers find themselves stuck between using raw HTTP clients and trying to shoehorn complex library calls into Eloquent models.

This guide will walk you through the recommended approach for integrating the Google Drive API into your Laravel application, focusing on best practices for authentication and service design.

The Right Way to Interact with Google APIs in Laravel

The original examples you found using the google-api-php-client are functional but are designed for standalone scripts. In a Laravel environment, we must adopt an architectural pattern that separates concerns: Controllers handle requests, Models handle database interaction (like Eloquent), and dedicated Service classes handle complex external API logic.

Directly executing complex Google API calls inside a Controller violates the Single Responsibility Principle. Therefore, the best approach is to create a dedicated Service Class to manage all communication with the Google APIs. This keeps your business logic clean and makes testing significantly easier—a core principle of good software design, much like the philosophy behind building scalable applications on platforms like Laravel.

Step 1: Setup and Authentication (The Crucial First Step)

Before writing any code, you need a way to authenticate your application to Google. For file storage operations, you will typically use OAuth 2.0. Since you are dealing with sensitive files, using a Service Account might be an alternative for server-to-server operations, although for user-uploaded content, user consent via OAuth is usually required.

  1. Google Cloud Console: Create a project and enable the Google Drive API.

  2. Credentials: Generate OAuth 2.0 Client IDs to get your client_id and client_secret.

  3. Installation: Use Composer to install the official or community-maintained PHP client library, such as the one you mentioned:

    composer require google/apiclient:^2.0
    
  4. Configuration: Store your credentials securely in your Laravel .env file.

Step 2: Creating a Dedicated Drive Service

We will create a service class to encapsulate all the API interaction logic. This keeps the complexity out of your controllers.

First, create the service:

php artisan make:service GoogleDriveService

Inside app/Services/GoogleDriveService.php, you would set up your dependency injection and the client initialization.

<?php

namespace App\Services;

use Google\Client;
use Google\Service\Drive;
use Exception;

class GoogleDriveService
{
    protected $driveService;

    public function __construct()
    {
        // Load credentials securely from environment variables
        $client = new Client();
        $client->setApplicationName('Laravel_Drive_App');
        $client->setDeveloperKey(env('GOOGLE_API_KEY')); // Use your API key here

        // Initialize the Drive service object
        $service = new Drive($client);
        $this->driveService = $service;
    }

    /**
     * Upload a file (e.g., a PDF) to Google Drive.
     */
    public function uploadFile(string $filePath, string $fileName): string
    {
        try {
            // Example: Reading the file content for upload
            $fileContent = file_get_contents($filePath);

            $fileMetadata = new \Google\Service\Drive\DriveFile([
                'name' => $fileName,
                'mimeType' => 'application/pdf', // Set the correct MIME type
            ]);

            // Execute the upload call
            $file = $this->driveService->files->create(
                $fileMetadata,
                $fileContent,
                ['uploadType' => 'media']
            );

            return $file->id; // Return the new file ID
        } catch (Exception $e) {
            // Log the error for debugging
            \Log::error("Google Drive Upload Failed: " . $e->getMessage());
            throw new \Exception("Failed to upload file to Google Drive.");
        }
    }
}

Step 3: Using the Service in a Controller

Finally, your Laravel controller becomes extremely clean. It simply delegates the heavy lifting to the service layer. This separation is what makes large applications manageable and scalable, allowing you to focus on sophisticated features instead of boilerplate API calls. When designing robust systems like those found at laravelcompany.com, this architectural discipline is essential for long-term success.

<?php

namespace App\Http\Controllers;

use App\Services\GoogleDriveService;
use Illuminate\Http\Request;

class FileUploadController extends Controller
{
    protected $driveService;

    public function __construct(GoogleDriveService $driveService)
    {
        $this->driveService = $driveService;
    }

    public function storePdf(Request $request)
    {
        // 1. Validate the uploaded file (assuming $request->file() is the PDF)
        if (!$request->hasFile('pdf_file')) {
            return response()->json(['error' => 'No file provided'], 400);
        }

        $uploadedFile = $request->file('pdf_file');
        $fileName = time() . '-' . $uploadedFile->getClientOriginalName();

        try {
            // 2. Use the service to handle the external API call
            $driveFileId = $this->driveService->uploadFile(
                $uploadedFile->path(), // Path to the temporary file on the server
                $fileName
            );

            return response()->json([
                'message' => 'File successfully uploaded to Google Drive.',
                'drive_id' => $driveFileId,
            ], 201);

        } catch (\Exception $e) {
            return response()->json(['error' => 'Upload failed: ' . $e->getMessage()], 500);
        }
    }
}

Conclusion

Integrating external APIs like the Google Drive API into a Laravel application is not about pasting raw PHP code; it’s about adopting sound software architecture. By leveraging dedicated Service Classes, secure credential management via environment variables, and proper dependency injection, you transform a brittle script into a robust, testable feature. This approach ensures that your application remains clean, maintainable, and scalable, allowing you to focus on delivering exceptional features for your users.