0

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 ?

CK Tan
  • 596
  • 2
  • 10
  • 25

2 Answers2

2

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();
}
?>
CK Tan
  • 596
  • 2
  • 10
  • 25
0

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

Emily
  • 1,464
  • 1
  • 9
  • 12
  • Hi, Emily. The API PHP client library is mainly for the services that available on Google API consoles but not for Admin SDK. They seem different. I cannot find any php client library for Admin SDK. Does it mean it's not supported by php at the moment ? – CK Tan Jan 08 '14 at 02:45
  • Admin SDK is actually just the name for the suite of all the APIs that's designed specifically for Administrators (Directory API, Reports API, Site Verification API... are all part of Admin SDK). From your question originally, you were interested in using the directory API (which is part of the Admin SDK). So if you take a look at the PHP client library, you will see the individual service including the directory service. – Emily Jan 08 '14 at 04:40
  • I can't find the php client library in Directory API : https://developers.google.com/admin-sdk/directory/v1/libraries Thanks for the helps again. – CK Tan Jan 08 '14 at 04:59
  • it is not in the page, but if you download the php client in the link I provided above. You will see it in the package. :) (i know it is confusing...) – Emily Jan 08 '14 at 05:16
  • Thanks Emily. I finally able to find the php file, the naming is just too confusing. Let me take a look at it first. Thanks agin. – CK Tan Jan 08 '14 at 05:38