create notification channel using google-api-php-client

15 Views Asked by At

I want to create a notification channel so that each time a user is deleted from my workspace my php app gets notified, I'm using the following

<?php

namespace App\Services;

use App\Models\Student;
use Exception;
use Google\Service\Directory\Channel;
use Illuminate\Support\Str;

class WorkspaceAdminService
{

    /*
     * Email address for admin user that should be used to perform API actions
     * Needs to be created via Google Apps Admin interface and be added to an admin role
     * that has permissions for Admin APIs for Users
     */
    protected string $delegatedAdmin;


    /*
     * Some name you want to use for your app to report to Google with calls, I assume
     * it is used in logging or something
     */
    protected string $appName;

    /*
     * Array of scopes you need for whatever actions you want to perform
     * See https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
     */
    protected $scopes = array(

        \Google\Service\Directory::ADMIN_DIRECTORY_USER,
        \Google\Service\Directory::ADMIN_DIRECTORY_ORGUNIT,
    );
    /*
     * Provide path to JSON credentials file that was downloaded from Google Developer Console
     * for Service Account
     */
    protected $authJson;
    /**
     * @var \Google\Service\Directory  $dir
     **/
    protected $dir;

    public function __construct()
    {
        $authJson = resource_path('jsons/eservices-emails-creds.json');
        $this->appName = env("GOOGLE_APP_NAME");
        $this->delegatedAdmin = env("GOOGLE_DELEGATED_ADMIN");
        /*
         * Create Google_Client for making API calls with
         */
        $googleClient = new \Google\Client();
        $googleClient->setApplicationName($this->appName);
        $googleClient->useApplicationDefaultCredentials();
        $googleClient->setAuthConfig($authJson);
        $googleClient->setSubject($this->delegatedAdmin);
        $googleClient->setScopes($this->scopes);
        $googleClient->setAccessType('offline');

        /*
         * Get an instance of the Directory object for making Directory API related calls
         */
        $this->dir = new \Google\Service\Directory($googleClient);
    }

   
    public function watch(string $url)
    {
        $channel = new Channel();

        $channel->setId(Str::uuid()->toString());
        $channel->setAddress($url);
        $channel->setType('web_hook');
        try {
            $this->dir->users->watch($channel, [
                'event' => 'delete',
            ]);

        } catch (Exception $e) {
            dd($e);
        }
    }
}

but it is throwing an exception with the following

{
      "error": {
        "code": 400,
        "message": "Bad Request",
        "errors": [
          {
            "message": "Bad Request",
            "domain": "global",
            "reason": "badRequest"
          }
        ]
      }
    }

note the jsons/eservices-emails-creds.json is the credentials file downloaded from the console and it works when adding a user like $this->dir->users->insert($user) but fails for notification channel creation

0

There are 0 best solutions below