Search code examples
phplaravelgoogle-gsuite

Gsuite integration in Laravel PHP


Our Client has the gsuite account. They need to do syn the gsuite employees to our application. how we will do it in php laravel ?


Solution

  • Package :

          "google/apiclient": "^2.0",
          "laravel/socialite": "^4.3",
    

    Redirection

            $redirectUrl = Config::get('services.google.redirect');                 
            return Socialite::driver('google')->with(["access_type" => "offline", "prompt" => "consent", "redirectUrl" =>  $redirectUrl])->scopes([Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY])->redirect(); 
    

    CallBack Get Employee:

            $client = new Google_Client();
            $client->setClientId(config('services.google.client_id'));
            $client->setClientSecret(config('services.google.client_secret'));
            $client->setApplicationName(env('APP_NAME'));
            $client->setDeveloperKey(env('GOOGLE_SERVER_KEY'));
            $client->setApprovalPrompt("consent");
            $client->setAccessType("offline");
            $client->setAccessToken($accessToken);
            
            $service = new Google_Service_Directory($client);
    
            $optParams = array(
                'customer' => 'my_customer',
                'maxResults' =>500,
                'orderBy' =>'givenName',
                'projection' => 'full',
                'showDeleted'=>true,
                'sortOrder' => 'descending',
                //'query' => 'creationTime=2001-02-15'
                // 'query' => 'isAdmin%3Dtrue'
            );
    
            if(!empty($pageToken)){
                $pageData = array("pageToken" => $pageToken);
                $optParams = array_merge($optParams, $pageData);
            }
            $results = $service->users->listUsers($optParams);