douyi6960 2014-01-26 14:59
浏览 102
已采纳

PHP中的Google Admin SDK

I am using the following code to retrieve the list of users associated with my google apps account. There is no problem with authentication but when the redirection made this error is appearing.

index.php

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

  $list_users = $adminService->users->listUsers();
  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 i'm getting:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling
GET https://www.googleapis.com/admin/directory/v1/users?
key=AIzaSyBp0yBFCCosu113tbNbw7yAIjIt1ndFFIs: (400) Bad Request' 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(695):     
Google_ServiceResource->__call('list', Array) #3         
/var/www/vhosts/vx44.com/httpdocs/test_user/test_user.php(52): 
Google_UsersServiceResource->listUsers('nelson302.com') #4 {main} thrown in/var/www/vhosts/vx44.com/httpdocs/test_user/src/io/Google_REST.php on line 66     

Note that i've enabled Admin SDK on Google APIs Console.

What am i doing wrong here? thank you for helping

  • 写回答

1条回答 默认 最新

  • dongyong8071 2014-01-29 20:03
    关注

    Try replacing:

    $list_users = $adminService->users->listUsers();
    

    with:

    $adminOptParams = array('customer' => 'my_customer');
    $list_users = $adminService->users->listUsers($adminOptParams);
    

    this is explained in the Admin SDK Developer's Guide.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R