0

Please someone help me with this. Why the Google_Service_Drive is undefined? I Has installed the google/apiclient and do the configuration on .env file but it still says its undefined.

I've been tryng to update the google/apiclient and composer but its says its already the latest version. i've trying to clear the config cache too buts its no use and i did 'dd(class_exists('Google_Client'), class_exists('Google_Service_Drive'));' to check the class is exist or no but nothing happend, maybe its because I dont know how to use it cause i'm never done this before.

2
  • Please do not upload images of code/data/errors. Commented Aug 5, 2024 at 19:16
  • Please add some more details to your question. What have you tried to do? What version are you using? And so on Commented Aug 6, 2024 at 0:54

2 Answers 2

1

I was stuck with this problem as well. I was trying to connect Google Drive with my Laravel project. My goal was to allow users to connect their Google Drive accounts so they can upload, download, and view a list of their files directly from the application.

To get the class :

remove the "_" and replace it with "\"
use Google_Client;
use Google\Service\Drive;
use Google\Client;




function listFiles($clientId)
    {
       $clientId = (int) $clientId;
       $client = $this->getClient($clientId);
       $service = new \Google\Service\Drive($client);
       $files = $service->files->listFiles([
                'pageSize' => 10,
                'fields' => 'files(id, name, mimeType)'
            ]);

       return $files->getFiles();
    }


function uploadToDrive(Request $request)
    {
        $uploadedFile = $request->file('file');
        $client = $this->getClient($request->client_id);

        $service = new \Google\Service\Drive($client);
        $fileMetadata = new \Google\Service\Drive\DriveFile([
                'name' => $uploadedFile->getClientOriginalName()
            ]);

        $file = $service->files->create($fileMetadata, [
                'data' => file_get_contents($uploadedFile->getRealPath()),
                'mimeType' => $uploadedFile->getClientMimeType(),
                'uploadType' => 'multipart',
                'fields' => 'id, name'
            ]);

        return $file;
    }


function downloadDriveFile(Request $request)
    {
      $client = $this->getClient($request->client_id);
      $service = new \Google\Service\Drive($client);
      $file = $service->files->get($request->file_id, ['fields' => 'name,   mimeType']);

      $response = $service->files->get($request->file_id, ['alt' => 'media']);

       return response()->streamDownload(function () use ($response) {
                echo $response->getBody();
            }, $file->getName(), [
                'Content-Type' => $file->getMimeType()
            ]);
    }


function getClient($clientId)
    {
        $integration = CloudIntegration::where('user_id', $clientId)
            ->where('provider', 'google_drive')->first();

        if (!$integration) {
            return $this->responseWithError(null, "Client not found", 404);
        }
        $token = json_decode($integration->access_token, true);
        // $token = $integration->access_token;

        $client = new Google_Client();
        $client->setClientId(config('services.google.client_id'));
        $client->setClientSecret(config('services.google.client_secret'));
        $client->setRedirectUri(config('services.google.redirect'));

        $client->setAccessToken($token);

        if ($client->isAccessTokenExpired()) {
            $newToken = $client->fetchAccessTokenWithRefreshToken($integration->refresh_token);
            $integration->update([
                'access_token' => json_encode($newToken),
                'access_token_expires_in' => now()->addSeconds($newToken['expires_in'])
            ]);
            $client->setAccessToken($newToken);
        }
        return $client;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I believe that format is an older format and the newer version uses namespaces, for example

require ("path_to_google_vendor/autoload.php")
//$client is \Google\Client
$service=new \Google\Service\Drive($client);

//Create a folder in root of google drive
$file=new \Google\Service\Drive();
$file->setMimeType('application/vnd.google-apps.folder');
$folder = $service->files->create($file);

A lot of old examples would use the _ version : Google_Service_Drive. For the entire google API you can replace the _ with a \ to get the namespaced version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.