我们的客户拥有gsuite帐户。他们需要将gsuite员工同步到我们的应用程序。如何在PHP的laravel中实现呢?
Package:
"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();
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);