使用 google-api-php-client 创建通知通道

问题描述 投票:0回答:1

我想创建一个通知通道,以便每次从我的工作区删除用户时我的 php 应用程序都会收到通知,我正在使用以下内容

<?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);
        }
    }
}

但它抛出以下异常

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

注意

jsons/eservices-emails-creds.json
是从控制台下载的凭证文件,在添加像
$this->dir->users->insert($user)
这样的用户时它可以工作,但创建通知通道失败

php laravel google-api-php-client google-workspace google-admin-sdk
1个回答
0
投票

尝试在参数数组中添加域:

$this->dir->users->watch($channel, [
  'event' => 'delete',
  'domain' => 'yourdomain.com'
]);

我将此类用于 $channel 变量:

$channel = new Google_Service_Directory_Channel();
© www.soinside.com 2019 - 2024. All rights reserved.