Laravel PHP 中的 Gsuite 集成

Gsuite integration in Laravel PHP

我们的客户有 gsuite 帐户。他们需要将 gsuite 员工同步到我们的应用程序。我们将如何在 php laravel 中完成?

套餐:

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

重定向

        $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(); 

回调获取员工:

        $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);