PHP code example of brickservers / gsuite

1. Go to this page and download the library: Download brickservers/gsuite 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/ */

    

brickservers / gsuite example snippets


'domain' => env('GOOGLE_WORKSPACE_DOMAIN', 'example.com'),
'credentials_path' => env('GOOGLE_WORKSPACE_CREDENTIALS_PATH', storage_path('credentials.json')),
'subject' => env('GOOGLE_WORKSPACE_SUBJECT'),
'scopes' => [
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.group',
],

use BrickServers\GoogleWorkspace\GoogleWorkspace;

// Via facade or service container
$workspace = app('google-workspace');

// Or using dependency injection
public function __construct(GoogleWorkspace $workspace)
{
    $this->workspace = $workspace;
}

use BrickServers\GoogleWorkspace\DTOs\UserDTO;

$user = new UserDTO(
    email: '[email protected]',
    givenName: 'John',
    familyName: 'Doe',
    password: 'SecurePassword123!',
    changePasswordAtNextLogin: true,
);

$created = $workspace->users()->create($user);

$user = $workspace->users()->get('[email protected]');

// With projection and view type
use BrickServers\GoogleWorkspace\Enums\UserProjection;
use BrickServers\GoogleWorkspace\Enums\UserViewType;

$user = $workspace->users()->get(
    '[email protected]',
    projection: UserProjection::FULL,
    viewType: UserViewType::ADMIN_VIEW,
);

$result = $workspace->users()->list(maxResults: 100);

$users = $result['users']; // Array of UserDTO
$nextPageToken = $result['nextPageToken']; // For pagination

$updates = new UserDTO(
    email: '[email protected]',
    givenName: 'Johnny',
    familyName: 'Doe',
);

$updated = $workspace->users()->update('[email protected]', $updates);

$workspace->users()->delete('[email protected]');

// Suspend
$workspace->users()->suspend('[email protected]');

// Unsuspend
$workspace->users()->unsuspend('[email protected]');

// Add alias
$workspace->users()->addAlias('[email protected]', '[email protected]');

// Remove alias
$workspace->users()->removeAlias('[email protected]', '[email protected]');

use BrickServers\GoogleWorkspace\DTOs\GroupDTO;

$group = new GroupDTO(
    email: '[email protected]',
    name: 'Development Team',
    description: 'All developers in the organization',
);

$created = $workspace->groups()->create($group);

$group = $workspace->groups()->get('[email protected]');

$result = $workspace->groups()->list(maxResults: 100);

$groups = $result['groups']; // Array of GroupDTO
$nextPageToken = $result['nextPageToken']; // For pagination

$updates = new GroupDTO(
    email: '[email protected]',
    name: 'Dev Team',
    description: 'Updated description',
);

$updated = $workspace->groups()->update('[email protected]', $updates);

$workspace->groups()->delete('[email protected]');

// Add member
$workspace->groups()->addMember('[email protected]', '[email protected]');

// Remove member
$workspace->groups()->removeMember('[email protected]', '[email protected]');

use BrickServers\GoogleWorkspace\Exceptions\GoogleWorkspaceException;

try {
    $workspace->users()->create($user);
} catch (GoogleWorkspaceException $e) {
    // Handle specific errors
    if ($e->getCode() === 5) {
        // Resource not found
    }
    
    logger()->error('Google Workspace API error: ' . $e->getMessage());
}

new UserDTO(
    email: string,
    givenName: string,
    familyName: string,
    password: ?string = null,
    changePasswordAtNextLogin: bool = true,
    suspended: bool = false,
    phone: ?string = null,
    title: ?string = null,
    customSchemas: ?array = null,
)

new GroupDTO(
    email: string,
    name: ?string = null,
    description: ?string = null,
)

UserProjection::BASIC    // Basic information only
UserProjection::FULL     // Full user information
UserProjection::CUSTOM   // Custom schema information

UserViewType::ADMIN_VIEW      // Admin perspective
UserViewType::DOMAIN_PUBLIC   // Public domain view

ApiScope::DIRECTORY_USER
ApiScope::DIRECTORY_GROUP
ApiScope::CLASSROOM_COURSES
ApiScope::CALENDAR
ApiScope::GMAIL_COMPOSE
ApiScope::DRIVE
// ... and more

GSuite::accounts()->create([
    ['first_name' => 'John', 'last_name' => 'Doe'],
    'email' => '[email protected]',
    'default_password' => 'password'
]);

use BrickServers\GoogleWorkspace\DTOs\UserDTO;

$user = new UserDTO(
    email: '[email protected]',
    givenName: 'John',
    familyName: 'Doe',
    password: 'password',
);

app('google-workspace')->users()->create($user);
bash
php artisan vendor:publish --provider="BrickServers\GoogleWorkspace\GoogleWorkspaceServiceProvider" --tag=config