Question

Thanks to Emily, I found the php client library already. I need to retrieve a users data. There is a function in the API that can do so :

Google_DirectoryService.php

/**
 * retrieve user (users.get)
 *
 * @param string $userKey Email or immutable Id of the user
 * @param array $optParams Optional parameters.
 * @return Google_User
 */
public function get($userKey, $optParams = array()) {
      $params = array('userKey' => $userKey);
      $params = array_merge($params, $optParams);
      $data = $this->__call('get', array($params));
      if ($this->useObjects()) {
        return new Google_User($data);
      } else {
        return $data;
      }
    }

index.php

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DirectoryService.php';

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

$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://mypage.com/oauth2callback');
$client->setDeveloperKey('xxx');

$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/admin.directory.user.readonly'));

$service = new Google_DirectoryService($client);
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
$results = $service->users->get('$email', 'public', $optParams); //Pass email to function parameter ? I'm not sure.

?>

I think the way I pass the parameter is wrong which i got error after adding the last line. How do I pass users login email to the function correctly ?

The error i got is :

[08-Jan-2014 03:17:33 America/Chicago] PHP Fatal error:  Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/admin/directory/v1/users/me?key=: (403) Insufficient Permission' in /home2/iaapro/public_html/php/google-api-php-client/src/io/Google_REST.php:66
Stack trace:
#0 /home2/iaapro/public_html/php/google-api-php-client/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest))
#1 /home2/iaapro/public_html/php/google-api-php-client/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest))
#2 /home2/iaapro/public_html/php/google-api-php-client/src/contrib/Google_DirectoryService.php(653): Google_ServiceResource->__call('get', Array)
#3 /home2/iaapro/public_html/php/google-plus-access.php(44): Google_UsersServiceResource->get('me')
#4 /home2/iaapro/public_html/php/index.php(2): include_once('/home2/iaapro/p...')
#5 {main}
  thrown in /home2/iaapro/public_html/php/google-api-php-client/src/io/Google_REST.php on line 66

In addition, I have checked "Enable API Access" on admin.google.com and enable Admin SDK on Google APIs Console.

Can anyone gives me some help please ?

Was it helpful?

Solution

Finally, I figured out how to call the array using Google directory service API. Please refer to the code below:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'google-api-php-client/src/contrib/Google_DirectoryService.php';

session_start();

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

//*********** Replace with Your API Credentials **************
$client->setClientId('Your_Client_ID');
$client->setClientSecret('Your_Client_Sercret');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('Your_API_Key');
//************************************************************

$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.readonly'));
$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

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

  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>

OTHER TIPS

Have you checked out the Google API PHP client library? (https://code.google.com/p/google-api-php-client/)

Also, they have a github page: https://github.com/google/google-api-php-client

Once you downloaded the library from the site, you can see that the library has the Google_DirectoryService

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