2014-08-27 106 views
1

我正嘗試將Google API PHP Client與Google Directory API配合使用。我進入Google Developers Console並創建了一個名爲google-sync的項目。然後我在API列表頁面中啓用Admin SDK。然後,我從「憑據」頁面中選擇「創建新客戶端ID」,並選擇Service Account,然後下載提示下載的.bin私鑰。然後,我點擊了「生成新的P12鍵」並下載了.p12文件,該文件與PHP文件放在同一目錄中。(400)使用Google API PHP客戶端和Admin SDK的錯誤請求

這是我的PHP代碼(其中this part of the docs)試圖列出所有用戶。

<?php 
session_start(); 
require 'vendor/autoload.php'; 
$SCOPE = 'https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/admin.directory.orgunit'; 

$SERVICE_ACCOUNT_EMAIL = '<EMAIL ADDRESS>'; 
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = '<P12 FILE NAME>.p12'; 

$client = new Google_Client(); 
$client->setApplicationName('google-sync'); 
$adminService = new Google_Service_Directory($client); 
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH); 
$cred = new Google_Auth_AssertionCredentials(
    $SERVICE_ACCOUNT_EMAIL, 
    array($SCOPE), 
    $key); 
$client->setAssertionCredentials($cred); 

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

當我運行這段代碼,我得到這個錯誤:

PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/admin/directory/v1/users: (400) Bad Request' in /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php:80 
Stack trace: 

#0 /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) 
#1 /projects/google-sync/vendor/google/apiclient/src/Google/Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) 
#2 /projects/google-sync/vendor/google/apiclient/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) 
#3 /projects/google-sync/vendor/google/apiclient/src/Google/Service/Directory.php(2063): Google_Service_Resource->call('list', Array, 'Google_Service_...') 
#4 /projects/google-sync/auth-test.php(20): Google_Service_Directory_Users_Resource->listUsers() 
#5 {main} 
    thrown in /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php on line 80 

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/admin/directory/v1/users: (400) Bad Request' in /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php on line 80 

Google_Service_Exception: Error calling GET https://www.googleapis.com/admin/directory/v1/users: (400) Bad Request in /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php on line 80 

Call Stack: 
    0.0001  232296 1. {main}() auth-test.php:0 
    0.0172 2957992 2. Google_Service_Directory_Users_Resource->listUsers() /projects/google-sync/auth-test.php:20 
    0.0172 2959144 3. Google_Service_Resource->call() /projects/google-sync/vendor/google/apiclient/src/Google/Service/Directory.php:2063 
    0.3356 2970752 4. Google_Client->execute() /projects/google-sync/vendor/google/apiclient/src/Google/Service/Resource.php:195 
    0.3356 2971568 5. Google_Http_REST::execute() /projects/google-sync/vendor/google/apiclient/src/Google/Client.php:499 
    0.7015 2974424 6. Google_Http_REST::decodeHttpResponse() /projects/google-sync/vendor/google/apiclient/src/Google/Http/REST.php:44 

當我下載了P12文件,我得到了與私有密鑰關聯的密碼,但我無法找到有關如何包含該密碼的任何文檔。這是我的問題嗎?

回答

0

我能夠通過進入我的域的管理控制檯,轉到安全性下的管理API客戶端訪問頁面,並從開發者控制檯添加客戶端ID並添加了我需要的範圍來修復此問題。

有關更多信息,請參見this part of the docs

1

我正面臨同樣的問題。服務帳戶在發出請求時應模擬域管理員。另外,listUsers()期望域作爲參數傳遞。

可以確保您已經把域範圍內的機關服務帳戶 - https://developers.google.com/api-client-library/php/auth/service-accounts

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    array($SCOPE), 
    $key 
); 
$cred->sub = "[email protected]"; 
//Get All users. 
$list = $service->users->listUsers(Array('domain' => 'domain.com')); 
//Get one user 
$userId = $service->users->get('[email protected]'); 
相關問題