2017-12-13 32 views
0

Google 관리자 SDK와 관련하여 도움이 필요합니다. 저는 Google Admin SDK에 대한 초보자입니다. 따라서 다음과 같이 승인하는 동안 표시 오류가 계속 발생합니다.Google Admin SDK 무단 클라이언트 PHP가 1 시간 후에 완벽하게 작동합니다.

{ error: "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method" }

여기 내 코드입니다.

<?php 

include_once '../vendor/autoload.php'; 
include_once "base.php"; 
session_start(); 
echo pageHeader("Service Account Access"); 


/************************************************ 
    Make an API request authenticated with a service 
    account. 
************************************************/ 
$client = new Google_Client(); 
$client->setAuthConfig('service_account.json'); 

$client->setApplicationName("automate user"); 
$client->setSubject('[email protected]******.org'); 

$client->setScopes(['https://www.googleapis.com/auth/admin.directory.user']); 

$client->setIncludeGrantedScopes(true); 
$client->setAccessType('offline'); 
//$auth_url = $client->createAuthUrl(); 

//header('Location: '.filter_var($auth_url, FILTER_SANITIZE_URL)); 

if(isset($_SESSION['access_token']) && $_SESSION['access_token']) 
{ 
    $client->setAccessToken($_SESSION['access_token']); 

    if ($credentials_file = checkServiceAccountCredentialsFile()) { 
    // set the location manually 
    $client->setAuthConfig('client_secret1.json'); 
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS=service_account.json')) { 
    // use the application default credentials 
    $client->useApplicationDefaultCredentials(); 
} else { 
    echo missingServiceAccountDetailsWarning(); 
    return; 
} 

$dir = new Google_Service_Directory($client); 
$user = new Google_Service_Directory_User(); 
$name = new Google_Service_Directory_UserName(); 


//$results = $dir->users->get('********@****.com'); 
$name->setGivenName('*****'); 
$name->setFamilyName('*****'); 
$user->setName($name); 
$user->setHashFunction('SHA-1'); 
$user->setPrimaryEmail('****@***'); 
$user->setpassword(hash('sha1','******')); 
$user->setchangePasswordAtNextLogin(false); 


$results = $dir->users->insert($user); 

if($results) { 
    echo "New user : ".$results->primaryEmail.""; 
} else{ 
    echo " User doesn't exist : ".$email; 
} 

} 
else { 

if ($client->isAccessTokenExpired()) { 
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
    //file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
    } 
    $redirect_uri = 'http://'.$_SERVER['HTTP_HOST']. '/google/oauth.php'; 
    header('Location: '.filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
} 


<?php pageFooter(__FILE__); ?> 

처음으로 완벽하게 작동합니다. 며칠 후이 코드를 실행하면 unauthorized_client이라는 오류가 표시됩니다. 나는이 오류가 왜 나타나는지 모른다. 이미 도메인 전체의 위임 서비스 계정을 만들었습니다. 도움을 내가 뭐하는 거지 알려주세요 잘못

답변

0

이 github의 포럼 시도 제공 할 수 있습니다 :

API access using own credentials (server to server flow)

Step 1 - Creating OAuth2 credentials

Follow the steps for the product you're using to generate a service account with a JSON key file, then come back to this page.

Step 2 - Setting up the client library

Under the [OAUTH2] section of your adsapi_php.ini file, insert the path to your JSON key file and set the scope for the ads API you're accessing. Insert the email account of the user you want to impersonate as, if any.

+0

나는 내가 JSON 키 관리자 디렉토리 API 및 이미 생성 된 서비스 계정을 사용하고를 승인 된 다음 범위를 사용하십시오. https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group, https : // www. googleapis.com/auth/admin.directory.group.member –