0

I am trying to modify and delete user accounts through Google API. Our objective is to use the following API in our webpage.

Delete User: DELETE https://www.googleapis.com/admin/directory/v1/users/userKey

While calling the above API url in browser we got error 401 "Login Required" We need guidance how to call google API URL for delete & rename user thru Google API.

Also we followed the steps given in the below URL. For creating Api keys and OAuth 2.0 client IDs. After that it requires user approval to access token

https://developers.google.com/admin-sdk/directory/v1/guides/authorizing

I got stuck to accessing the token as well.

Kindly guide us with the steps to call the Google API

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
dhana
  • 1
  • 1

1 Answers1

0

See: https://developers.google.com/admin-sdk/directory/v1/reference/users/update

Assuming you are doing this administratively, you could use a domain wide delegated service account.

https://developers.google.com/api-client-library/php/auth/service-accounts

This example shows how an name could be updated for an existing user.

$impersonateUser    = 'adminWithRolesToMakeChanges@mydomain.edu';

define('SCOPES', implode(' ', array( Google_Service_Directory::ADMIN_DIRECTORY_USER ) ));
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . '/path/creds.json' );

$client = new Google_Client();

$client->useApplicationDefaultCredentials(); 
$client->setScopes(SCOPES); 
$client->setSubject($impersonateUser); 

$dir = new Google_Service_Directory($client);

$gPrimaryEmail = 'joeschmo1@mydomain.edu';
$firstName     = 'Frank';
$lastName      = 'Johnson';

$gNameObject = new Google_Service_Directory_UserName(
                      array(
                         'familyName' =>  $lastName,
                         'givenName'  =>  $firstName,
                         'fullName'   =>  "$firstName $lastName"));


$gUserObject = new Google_Service_Directory_User( 
                      array( 
                         'name' => $gNameObject));

$results = $dir->users->update($gPrimaryEmail, $gUserObject);

print_r($results);
Jay Fowler
  • 65
  • 6