【问题标题】:Display Google Admin SDK JSON Response in PHP在 PHP 中显示 Google Admin SDK JSON 响应
【发布时间】:2014-01-25 09:19:50
【问题描述】:

感谢 Emily,我已经找到了 php 客户端库。我需要检索用户数据。 API中有一个函数可以做到这一点:

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.

?>

我认为我传递参数的方式是错误的,在添加最后一行后出现错误。如何正确地将用户登录电子邮件传递给函数?

我得到的错误是:

[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

此外,我在 admin.google.com 上勾选了“启用 API 访问”,并在 Google API 控制台上启用了 Admin SDK。

谁能帮帮我?

【问题讨论】:

  • 我用来做测试的站点是php.s3uc.com

标签: php json google-admin-sdk


【解决方案1】:

最后,我想出了如何使用 Google 目录服务 API 调用数组。请参考以下代码:

<?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();
}
?>

【讨论】:

    【解决方案2】:

    您是否查看过 Google API PHP 客户端库? (https://code.google.com/p/google-api-php-client/)

    另外,他们有一个 github 页面:https://github.com/google/google-api-php-client

    从网站下载库后,您可以看到该库具有 Google_DirectoryService

    【讨论】:

    • 嗨,艾米丽。 API PHP 客户端库主要用于 Google API 控制台上可用的服务,但不适用于 Admin SDK。他们似乎不同。我找不到 Admin SDK 的任何 php 客户端库。是不是表示目前php不支持?
    • Admin SDK 实际上只是专门为管理员设计的所有 API 套件的名称(目录 API、报告 API、站点验证 API...都是 Admin SDK 的一部分)。从您最初的问题来看,您对使用目录 API(它是 Admin SDK 的一部分)感兴趣。因此,如果您查看 PHP 客户端库,您将看到包括目录服务在内的单个服务。
    • 我在 Directory API 中找不到 php 客户端库:developers.google.com/admin-sdk/directory/v1/libraries 再次感谢您的帮助。
    • 它不在页面中,但是如果您在我上面提供的链接中下载了php客户端。您将在包装中看到它。 :)(我知道这很令人困惑......)
    • 谢谢艾米丽。我终于能够找到php文件,命名太混乱了。让我先看看它。再次感谢。
    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 2014-05-02
    • 2018-10-12
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多