2014-04-24 3 views
0

Google API 콘솔에 APP가 있습니다. Admin SDK와 Marketplace SDK가 활성화되어 있습니다. 서비스 계정으로 등록하고 키 파일 등을 가지고 있습니다. 특정 도메인에서 사용자를 얻으려고하면 항상 하나의 메시지가 표시됩니다. "GET https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com을 호출하는 중 오류가 발생했습니다 : (403)이 액세스 권한이 없습니다. 리소스/api ". 내가 가지고있는 코드는 다음과 같습니다.Google API - 서비스 디렉토리 사용

$client = new Google_Client(); 
    $client->setApplicationName("Client_User_Feed");    

    $key = file_get_contents('/path/to/key/key-file-privatekey.p12'); 
    $cred = new Google_Auth_AssertionCredentials(
     '{code}@developer.gserviceaccount.com', 
     array('https://www.googleapis.com/auth/admin.directory.user'), 
     $key 
    ); 
    $client->setAssertionCredentials($cred); 
    $service = new Google_Service_Directory($client);   

    $users = $service->users->listUsers(array('domain' => 'mydomain.com')); 

이 문제를 어떻게 해결할 수 있습니까?

$adminUser = '[email protected]'; $cred->sub = $adminUser;

예제 코드 가져 오는 사용자 ID :

+0

이 도메인의 관리 콘솔에서 응용 프로그램의 범위를 추가 했습니까? – radia

+0

Marketplace SDK admin - ttps : //www.googleapis.com/auth/admin.directory.user – Deez

답변

0

당신은 같은과 관리자 사용자를 가장 할 필요가

$client_id = '{code}.apps.googleusercontent.com'; //Client ID from Developers Console 
$service_account_name = '{code}@developer.gserviceaccount.com'; //Email Address from Developers Console 
$key_file_location = '{path}{file}.p12'; //Path to the P12 key downloaded from Developers Console 
$impersonateUser = '[email protected]'; //The user's account we are fetching information from 

    try { 
    $client = new Google_Client(); //Instantiate the Google Client 
    $client->setApplicationName("ApplicationName"); 

    $adminService = new Google_Service_Directory($client); 

    $key = file_get_contents($key_file_location); 
    $cred = new Google_Auth_AssertionCredentials( //Instantiate the Auth class 
     $service_account_name, 
     array('https://www.googleapis.com/auth/admin.directory.user'),   //Set the scope 
     $key 
    ); 

    $adminUser = '[email protected]'; 
    $cred->sub = $adminUser; //The sub function of Auth lets us impersonate a user so that our service account ($client_id) can act on the user's behalf 

    $client->setAssertionCredentials($cred); 
    if ($client->getAuth()->isAccessTokenExpired()) { 
     $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 

    $getUser = getUserId($adminService, $impersonateUser); 
    $impersonateUser = $getUser['primaryEmail']; 

    if (isset($impersonateUser) && !empty($impersonateUser)) { 
     $_SESSION['gmailUserID'] = $impersonateUser; 
    } 
    //echo $_SESSION['gmailUserID'] . "<br />"; 
    } catch (Exception $e) { 
     LogErr($e); 
    } 

function getUserId($adminService, $impersonateUser) { 
    try { 
    $userId = $adminService->users->get($impersonateUser); 
    return $userId; 
    } catch (Exception $e) { 
    LogErr($e); 
    } 
} 
+0

에서이 범위를 사용할 수 있습니다. 관리자 계정으로 가장은 작동하지만 가장으로 결과를 얻을 수 있는지 궁금하십니까? – pdolinaj