-
-
Save WyattCast44/4c45b334c72fce4c6c3a3a6e1bc11a98 to your computer and use it in GitHub Desktop.
Example of using Google Admin SDK Directory API to get an individual or list of users.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Updated 2018-01-24 to work with google/apiclient:^2.0 | |
/* | |
* Easiest to use composer to install google-api-php-client and generate autoloader | |
* If you dont want to use composer you can manually include any needed files | |
*/ | |
include_once 'vendor/autoload.php'; | |
/* | |
* Email address for admin user that should be used to perform API actions | |
* Needs to be created via Google Apps Admin interface and be added to an admin role | |
* that has permissions for Admin APIs for Users | |
*/ | |
$delegatedAdmin = '[email protected]'; | |
/* | |
* Some name you want to use for your app to report to Google with calls, I assume | |
* it is used in logging or something | |
*/ | |
$appName = 'Example App'; | |
/* | |
* Array of scopes you need for whatever actions you want to perform | |
* See https://developers.google.com/admin-sdk/directory/v1/guides/authorizing | |
*/ | |
$scopes = array( | |
'https://www.googleapis.com/auth/admin.directory.user' | |
); | |
/* | |
* Provide path to JSON credentials file that was downloaded from Google Developer Console | |
* for Service Account | |
*/ | |
$authJson = __DIR__ . '/creds.json'; | |
/* | |
* Create Google_Client for making API calls with | |
*/ | |
$googleClient = new \Google_Client(); | |
$googleClient->setApplicationName($appName); | |
$googleClient->setAuthConfig($authJson); | |
$googleClient->setSubject($delegatedAdmin); | |
$googleClient->setScopes($scopes); | |
$googleClient->setAccessType('offline'); | |
/* | |
* Get an instance of the Directory object for making Directory API related calls | |
*/ | |
$dir = new \Google_Service_Directory($googleClient); | |
/* | |
* Get specific user example | |
*/ | |
//$account = $dir->users->get('[email protected]'); | |
//print_r($account); | |
/* | |
* Get list of users example | |
* In my testing you must include a domain, even though docs say it is optional | |
* I was getting an error 400 without it | |
*/ | |
$list = $dir->users->listUsers(array('domain' => 'domain.org', 'maxResults' => 500)); | |
print_r($list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment