0

Google アプリのドメインからすべてのユーザーを取得する方法。次の共学を試してみましたが、「GET https://www.googleapis.com/admin/directory/v1/users?domain=domainname.comの呼び出し中にエラーが発生しました: (401) ログインが必要です」のような Google リターン エラーが返されます。

set_include_path(get_include_path() . PATH_SEPARATOR . 'E:\wamp\www\Google APIs\google-api-php-client\src');
ini_set("memory_limit", -1);
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Drive.php';
require_once 'google-api-php-client/src/Google/Service/Oauth2.php';
require_once 'google-api-php-client/src/Google/Service/Directory.php';

$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
$client->setClientId('42X74XXXXXercontent.com');
$client->setClientSecret('OxzVALdXwd');
$client->setRedirectUri('http://localhost/Google%20APIs/index.php');
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setDeveloperKey("AIXXXXXJDUdX48");
$SCOPES = array(
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile');
$client->setScopes($SCOPES);
$token = '{"access_token":"ya29.1.AAXXXaeTg","token_type":"Bearer","expires_in":3600,"id_token":"eyJhbXXXXXXX9cEjDMUMe6PVrgo","refresh_token":"1\/sN-XXXXX5qdIGMeEW95tJv-0a1ujWk","created":1397878356}';
$decoded = json_decode($token);
$client->setAccessToken($token);
if ($client->isAccessTokenExpired()) {
    $client->refreshToken($decoded->refresh_token);
}
$adminsdk = new Google_Service_Directory($client);
$users_list = $adminsdk->users->listUsers(array('domain'='domainon.info'));

注:私は承認を実行しました

$SCOPES = array(
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile');

私を助けてください

4

1 に答える 1

0

それらのスコープの承認を実行しているとあなたは言いますが、それらのスコープはクライアントに含まれていません。を実行する前に、必ず次のコードを含めてください$client->refreshToken()

$client->setScopes($SCOPES);
于 2014-04-23T14:31:05.310 に答える