PHP无法通过Google Directory API 2.0更新用户密码

时间:2017-01-27 12:49:10

标签: php google-api google-oauth google-api-php-client google-directory-api

我们从此网址(https://developers.google.com/admin-sdk/directory/v1/quickstart/php)执行了以下步骤。

列出用户正在工作!

但是在更新给定userKey的新密码时,我们收到以下错误:

  

PHP Catchable致命错误:参数2传递给   Google_Service_Directory_Resource_Users :: update()必须是一个实例   of Google_Service_Directory_User,没有给出,调用   第85行的/var/www/passwords_change/danish/quickstart.php并定义   在   /var/www/passwords_change/danish/vendor/google/apiclient-services/src/Google/Service/Directory/Resource/Users.php   在第162行

我附上了我的代码。

注意:我们拥有有效的凭据......!

<?php
require_once __DIR__ . '/vendor/autoload.php';


define('APPLICATION_NAME', 'Directory API PHP Quickstart');
define('CREDENTIALS_PATH', '/home/ramesh/.credentials/admin-directory_v1-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/admin-directory_v1-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY)
));

if (php_sapi_name() != 'cli') {
  throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfig(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }
  return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Directory($client);
$optParams = array(
  'userKey' => 'danish@XXXX.in',
);

$password = crypt ( "Password", $salt="IamSecretkey" );
$obj = new Google_Service_Directory_User;

try{
    $results = $service->users->update($optParams );
} catch(Error $ex) {
print_r($ex->getMessage());
}
echo "<pre>";
print_r($results);
exit;


/*
// Print the first 10 users in the domain.
$optParams = array(
  'customer' => 'my_customer',
  'maxResults' => 10,
  'orderBy' => 'email',
);
$results = $service->users->listUsers($optParams);

if (count($results->getUsers()) == 0) {
  print "No users found.\n";
} else {
  print "Users:\n";
  foreach ($results->getUsers() as $user) {
    printf("%s (%s)\n", $user->getPrimaryEmail(),
        $user->getName()->getFullName());
  }
}
*/

1 个答案:

答案 0 :(得分:1)

你几乎做对了。您出错的部分是创建用户资源对象并调用API。逻辑应该类似于:

#

请参阅official documentation以获得更好,更详细的说明。我希望这有帮助!