2016-07-22 161 views
0

我正在使用Google Directory API,oauth2的服務帳戶和PHP,從命令行運行。Google Directory API和PHP:未經授權的客戶端或請求範圍

作爲第一步,我只是試圖檢索一個用戶對象。下面是代碼:

require_once realpath(__DIR__ . '/../vendor/autoload.php'); 
session_start(); 
define('APPLICATION_NAME', 'Directory API PHP Quickstart'); 
define('SCOPES', implode(' ', 
array(Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY))); 
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/XXXXXXXX.json'); 
$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->addScope(SCOPES); 
$client->setSubject('[email protected]'); 
$httpClient = $client->authorize(); 
$response = 
$httpClient->get('https://www.googleapis.com/admin/directory/v1/users/[email protected]'); 
print $response->getBody(); 

這是我得到的迴應:

Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `401 Unauthorized` response: 
{ 
"error": "unauthorized_client", 
"error_description": "Unauthorized client or scope in request." 
} 

' in /var/www/feeds/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107 
Stack trace: #0 /var/www/feeds/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) 
#1 /var/www/feeds/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) 
#2 /var/www/feeds/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) 
#3 /var/www/feeds/vendor/guzzlehttp/promises/src/TaskQueue.php(61): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() 
#4 /var/www/feeds/vendor/guzzlehttp/promis in /var/www/feeds/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 107 

在管理控制檯>安全性>管理API客戶端訪問,我已經進入了客戶端ID使用這些API範圍:

View and manage the provisioning of groups on your domain 
https://www.googleapis.com/auth/admin.directory.group 
View and manage the provisioning of users on your domain 
https://www.googleapis.com/auth/admin.directory.user 
Groups Settings 
https://www.googleapis.com/auth/apps.groups.settings 

我用於該主題的帳戶('[email protected]')是域上的超級管理員。服務帳戶已啓用域範圍委派。

我在這裏忽略了什麼?任何幫助,將不勝感激。

回答

0

檢查您是否在管理控制檯中啓用了API,同時檢查您的Google Project for Admin SDK是否啓用了服務。

相關問題