2016-07-22 3 views
0

oauth2의 서비스 계정 인 Google Directory API 및 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 클라이언트 액세스 관리, 나는이 API 스코프와 클라이언트 ID를 입력 :

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 Admin SDK 프로젝트에서 서비스가 활성화되어 있는지 확인합니다.