PHP code example of csenayeem025 / google-workspace-sdk
1. Go to this page and download the library: Download csenayeem025/google-workspace-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
csenayeem025 / google-workspace-sdk example snippets
// Initialized Client with `connection_key` parameter
$google_workspace_api = new \Glamstack\GoogleWorkspace\ApiClient('workspace');
// Retrieves a paginated list of either deleted users or all users in a domain.
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
$records = $google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users');
// Retrieves a paginated list of either deleted users or all users in a domain
// with query parameters m Google Workspace
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get
$user_key = '[email protected]';
$record = $google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/'.$user_key);
// Create new Google Workspace User
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/insert
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users#User
$record = $google_workspace_api->rest()->post('https://admin.googleapis.com/admin/directory/v1/users', [
'name' => [
'familyName' => 'Libby',
'givenName' => 'Kate'
],
'password' => 'ac!dBurnM3ss3sWithTheB4$t',
'primaryEmail' => '[email protected]'
]);
// Update an existing Google Workspace User
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/update
$user_key = '[email protected]';
$record = $google_workspace_api->rest()->put('https://admin.googleapis.com/admin/directory/v1/users/'.$user_key, [
'name' => [
'givenName' => 'Libby-Murphy'
]
]);
// Delete a Google Workspace User
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/delete
$user_key = '[email protected]';
$record = $google_workspace_api->rest()->delete('https://admin.googleapis.com/admin/directory/v1/users/'.$user_key);
// Initialize the SDK using the `test` configuration from `glamstack-google-workspace.php`
$client = new Glamstack\GoogleWorkspace\ApiClient('test');
// Get service account from your model (`GoogleServiceAccount` is an example)
$service_account = \App\Models\GoogleServiceAccount::where('id', '123456')->firstOrFail();
// Get JSON key string from database column that has an encrypted value
$json_key_string = decrypt(json_decode($service_account->json_key));
$client = new \Glamstack\GoogleWorkspace\ApiClient(null, [
'api_scopes' => [
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.googleapis.com/auth/contacts'
],
'customer_id' => config('tests.connections.test.customer_id'),
'domain' => config('tests.connections.test.domain'),
'json_key' => $json_key_string,
'log_channels' => ['single'],
'subject_email' => config('tests.connections.test.subject_email')
]);
// Get service account from your model (`GoogleServiceAccount` is an example)
$service_account = \App\Models\GoogleServiceAccount::where('id', '123456')->firstOrFail();
dd(decrypt(json_decode($service_account->json_key));
// {
// "type": "service_account",
// "project_id": "project_id",
// "private_key_id": "key_id",
// "private_key": "key_data",
// "client_email": "[email protected]",
// "client_id": "123455667897654",
// "auth_uri": "https://accounts.google.com/o/oauth2/auth",
// "token_uri": "https://oauth2.googleapis.com/token",
// "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
// "client_x509_cert_url": "some stuff"
// }
// Get a list of Google Workspace Users
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
$records = $google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users');
// Get a specific Google Workspace User
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get
$user_key = '[email protected]';
$record = $google_workspace_api->get('https://admin.googleapis.com/admin/directory/v1/users/' . $userKey);
// Retrieves a paginated list of either deleted users or all users in a domain
// with query parameters oogle.com/admin-sdk/directory/reference/rest/v1/users/list#OrderBy
// https://developers.google.com/admin-sdk/directory/v1/guides/search-users
$records = $google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users',[
'maxResults' => '200',
'orderBy' => 'EMAIL'
]);
// This will parse the array and render the query string
// https://admin.googleapis.com/admin/directory/v1/users?maxResults='200'&orderBy='EMAIL'