【问题标题】:Google_ServiceException error when retrieving list of users (using Google API SDK)检索用户列表时出现 Google_ServiceException 错误(使用 Google API SDK)
【发布时间】:2014-03-04 23:52:23
【问题描述】:

我正在使用以下代码检索与我的 Google Apps 管理员帐户关联的用户列表。使用 Google 应用程序管理员帐户时可以正常工作,但使用其他 Google 应用程序/Gmail 帐户时会出现错误。

代码:

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

错误:

致命错误:未捕获的异常“Google_ServiceException”和消息“调用 GET 时出错” https://www.googleapis.com/admin/directory/v1/users/william.nelson920@gmail.com?key=AIzaSyBp0yBFCCosu113tbNbw7yAIjIt1ndFFIs: (404) 没有资源 在 /var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php:66 中找到:userKey' 堆栈跟踪:#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} /var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php 第 66 行

【问题讨论】:

    标签: php json google-apps google-admin-sdk google-directory-api


    【解决方案1】:

    Directory API 仅限 Google Apps 管理员使用。它允许域管理员检索域用户的信息。

    您应该能够从您自己的域(并且仅限您自己的域)获取用户信息。在您的情况下,您正在尝试获取“william.nelson920@gmail.com”的用户信息。由于 gmail.com 是消费者 Google Apps 产品,我认为您不是 gmail.com 的管理员吗? API 抛出了正确的错误,表明该用户在您的域中不存在。

    这里是有关 Google 文档中获取请求的更多信息

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      相关资源
      最近更新 更多