Google Suite Rest APi update user photo PHP

149 Views Asked by At

I am trying to update the user photo for a G Suite user using the following code:

this->api_client = new Google_Client();
$this->api_client->setScopes([
   Google_Service_Directory::ADMIN_DIRECTORY_USER,
   Google_Service_Directory::ADMIN_DIRECTORY_GROUP,
   Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS,
   Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER,
   Google_Service_Directory::ADMIN_DIRECTORY_ORGUNIT,
]);
$this->setAuthentication();
$this->service = new Google_Service_Directory($this->api_client);

$image_url = $agent->getProfilePic();
$image = file_get_contents($image_url);
$encoded = base64_encode($image);
//web-safe encoded
$web_safe = strtr($encoded, '+/', '-_');

$user = new Google_Service_Directory_User();
$user->setThumbnailPhotoUrl($image_url);

$this->service->users->update([user_email], $user);

i get a 200 response, but when checking on the admin console, nothing changed, user still doesnt have a profile picture.

i try sending the picture not as web-safe, and also tried replacing "+/=" with "-_." but nothing worked

The documentation is really poor, so i am out of tracks

1

There are 1 best solutions below

0
On

Instead of going through the users, try users_photos, the example below functions.

$image contains base64_encoded jpeg image.

    $params=[
            'photoData'=>$image,
        ];
        $user = new \Google_Service_Directory_UserPhoto($params);
        $this->service->users_photos->update($user_email,$user);