Question

I am using the following code to retrieve the list of users associated with my Google apps admin account. It's working fine when using a Google apps admin account but when using other Google apps/Gmail accounts an error appears.

Code:

<?php
require_once 'test_user/src/Google_Client.php';
require_once 'test_user/src/contrib/Google_PlusService.php';
require_once 'test_user/src/contrib/Google_Oauth2Service.php';
require_once 'test_user/src/contrib/Google_DirectoryService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("ApplicationName");

//*********** Replace with Your API Credentials **************
$client->setClientId('****');
$client->setClientSecret('****');
$client->setRedirectUri('****');
$client->setDeveloperKey('****');
//************************************************************

$client->setScopes(array('https://www.googleapis.com/auth/plus.me     https://www.googleapis.com/auth/userinfo.email     https://www.googleapis.com/auth/admin.directory.user'));
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address
$adminService = new Google_DirectoryService($client); // Call directory API

error_reporting(E_ALL);
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken())
{
  $user = $oauth2->userinfo->get();
  $me = $plus->people->get('me');
  $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2

  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
  $users = $adminService->users->get($email);
  //print_r($users);
  //$list_users = $adminService->users->listUsers();
  $adminOptParams = array('customer' => 'my_customer');
  $list_users = $adminService->users->listUsers($adminOptParams);
  print '<h2>Response Result:</h2><pre>' . print_r($list_users, true) . '</pre>';
  $_SESSION['access_token'] = $client->getAccessToken();
}
else
{
  $authUrl = $client->createAuthUrl();
  header("location:$authUrl");
}
?>

Error:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/admin/directory/v1/users/william.nelson920@gmail.com?key=AIzaSyBp0yBFCCosu113tbNbw7yAIjIt1ndFFIs: (404) Resource Not Found: userKey' in /var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php:66 Stack trace: #0 /var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /var/www/vhosts/vx44.com/httpdocs/test_user/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /var/www/vhosts/vx44.com/httpdocs/test_user/src/contrib/Google_DirectoryService.php(653): Google_ServiceResource->__call('get', Array) #3 /var/www/vhosts/vx44.com/httpdocs/test_user/test_user.php(54): Google_UsersServiceResource->get('william.nelson9...') #4 {main} thrown in /var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php on line 66

Was it helpful?

Solution

The Directory API is restricted for Google Apps Admin only. It allows domain administrators to retrieve domain users' information.

You should be able to get user information from your own domain (and your own domain ONLY). In your case, you are trying to get the user information of 'william.nelson920@gmail.com'. Since gmail.com is a consumer Google Apps product, and I don't think you are the administrator of gmail.com? The API is throwing the correct error indicating that this user does not exist in your domain.

Here is more info about the get request from Google documentation

https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#get_user

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top