Categories: Web Development
On occasion, you want to build an application to interact with G Suite without logging to the console. The G Suite Admin SDK is a tool for the purpose.
In authentication, there are two ways to do it.
- Using service account and impersonate an account with administrative access.
- Use OAuth to authenticate access through SDK. (Recommended)
The first option is great for automate tasks, such as generating reports, but not recommended for web app with access from anyone.
Let’s look at how to implement option #2 with Laravel.
- Use composer to download and include the Admin SDK.
composer require google/apiclient:2.0
- Create a OAuth 2.0 client ID in https://console.developers.google.com/apis
- Place the
client_secretthe same way you when setting up Socialite using Google OAuth.
- In the Socialite login code, set scopes that you want the account with administrative access. The Admin SDK scopes are listed in here. For example to retrieve group, group alias, and member information, uses:
- When retrieving user detail after login, store the
6. Back to using Google client, create a
$client as below
$client = new Google_Client();
$client->setApplicationName('G Suite Directory Demo');
$client->setAccessToken([User Token here]);
$service = new Google_Service_Group($client);
$optParams = array(
'userKey' => 'email@example.com',
'maxResult' => 10
$results = $service->groups->listGroups($optParams);