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

return [
    'connections' => [
        'test' => [
            'api_scopes' => [
                'https://www.googleapis.com/auth/admin.directory.group',
                'https://www.googleapis.com/auth/admin.directory.user'
            ],
            'json_key_file_path' => storage_path(env('GOOGLE_WORKSPACE_TEST_JSON_KEY_FILE_PATH')),
            'log_channels' => ['single'],
            'customer_id' => env('GOOGLE_WORKSPACE_TEST_CUSTOMER_ID'),
            'domain' => env('GOOGLE_WORKSPACE_TEST_DOMAIN'),
            'subject_email' => env('GOOGLE_WORKSPACE_TEST_SUBJECT_EMAIL'),
            'test_group_email' => env('GOOGLE_WORKSPACE_TEST_GROUP_EMAIL')
        ],
    ]
]

$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_file_path' => storage_path('keys/glamstack-google-workspace/test.json'),
    '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();

// 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"
// }
  
    'channels' => [  
        // Add anywhere in the `channels` array  
        'glamstack-google-workspace' => [
            'name' => 'glamstack-google-workspace',
            'driver' => 'single',
            'level' => 'debug',
            'path' => storage_path('logs/glamstack-google-workspace.log')
        ]
    ],  
  
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => [
                'single','slack', 'glamstack-google-workspace'
            ],
            'ignore_exceptions' => false
        ]
    ],  
  
// Initialize the SDK  
$api_client = new \Glamstack\GoogleWorkspace\ApiClient('workspace');
$response = $api_client->rest()->get('https://admin.googleapis.com/admin/directory/v1/users');
  
$google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users');  
  
$endpoint = 'https://admin.googleapis.com/admin/directory/v1/users';  
$records = $google_workspace_api->rest()->get($endpoint);  
  
// 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'  
  
// 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);  
  
  
  
use Glamstack\GoogleWorkspace\ApiClient;  
  
class GoogleWorkspaceUserService  
{  
    protected $google_workspace_api;  
    public function __construct() {
        $this->google_workspace_api = new \Glamstack\GoogleWorkspace\ApiClient();
    }  
    
    public function listUsers(array $query = []) : object
    {
        $users = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users', $query);
        return $users->object;
    }
    
    public function getUser(string $user_key, array $query = []) : object
    {
        $user = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key, $query);
        return $user->object;
    } 
    
    public function storeUser(string $user_key, array $request_data = []) : object
    {
        $response = $this->google_workspace_api->rest()->post('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key, $request_data);
        return $response->object;
    }

    public function updateUser(string $user_key, array $request_data = []) : object
    {
        $response = $this->google_workspace_api->rest()->put('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key, $request_data);
        return $response->object;
    } 

    public function deleteUser(string $user_key) : bool
    {
        $response = $this->google_workspace_api->rest()->delete('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);
        return $response->status->successful;
    }
}  
  
// API Request  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
  
// API Response  
$response->headers; // object  
$response->json; // json  
$response->object; // object  
$response->status; // object  
$response->status->code; // int (ex. 200)  
$response->status->ok; // bool  
$response->status->successful; // bool  
$response->status->failed; // bool  
$response->status->serverError; // bool  
$response->status->clientError; // bool  
  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
$response->headers;  
  
$headers = (array) $response->headers;  
$content_type = $headers['Content-Type'];  
  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
$response->json;  
  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
$response->object;  
  
{#1256  
  +"kind": "admin#directory#user"
  +"id": "1111111111111"  
  +"etag": ""nMRgLWac8h8NyH7Uk5VvV4DiNp4uxXg5gNUd9YhyaJE/MgKWL9SwIVWCY7rRA988mR8yR-k""  
  +"primaryEmail": "[email protected]"  
  +"name": {#1242  
    +"givenName": "Kate"    
    +"familyName": "Libby"    
    +"fullName": "Kate Libby"  
  }  
  +"isAdmin": true  
  +"isDelegatedAdmin": false  
  +"lastLoginTime": "2022-01-18T15:26:16.000Z"  
  +"creationTime": "2021-12-08T13:15:43.000Z"  
  +"agreedToTerms": true  
  +"suspended": false  
  +"archived": false  
  +"changePasswordAtNextLogin": false  
  +"ipWhitelisted": false  
  +"emails": array:3 [
    0 => {#1253  
      +"address": "[email protected]"      
      +"type": "work"    
    }  
    1 => {#1258  
      +"address": "[email protected]"      
      +"primary": true  
    }  
    2 => {#1259  
      +"address": "[email protected]"
    }  
  ]  
  +"phones": array:1 [    
    0 => {#1247  
      +"value": "5555555555"      
      +"type": "work"    
    }  
  ]  
  +"languages": array:1 [    
    0 => {#1250  
      +"languageCode": "en"      
      +"preference": "preferred"    
    }  
  ]
  +"nonEditableAliases": array:1 [  
    0 => "[email protected]"  
  ]  
  +"customerId": "C000aaaaa"  
  +"orgUnitPath": "/"  
  +"isMailboxSetup": true  
  +"isEnrolledIn2Sv": false  
  +"isEnforcedIn2Sv": false  
  +"
  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
$response->status;  
  
{  
  +"code": 200  
  +"ok": true  
  +"successful": true  
  +"failed": false  
  +"serverError": false  
  +"clientError": false  
}  
  
$response = $this->google_workspace_api->rest()->get('https://admin.googleapis.com/admin/directory/v1/users/' . $user_key);  
$response->status->code;  
bash
php artisan vendor:publish --tag=glamstack-google-workspace
bash
cp config/glamstack-google-workspace.php config/glamstack-google-workspace.php.bak

php artisan vendor:publish --tag=glamstack-google-workspace