Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过将PHP脚本(CI控制器)作为cronjob运行,在Google日历中插入特定用户的新事件(使用PHP API)_Php_Codeigniter_Google Calendar Api_Google Oauth - Fatal编程技术网

通过将PHP脚本(CI控制器)作为cronjob运行,在Google日历中插入特定用户的新事件(使用PHP API)

通过将PHP脚本(CI控制器)作为cronjob运行,在Google日历中插入特定用户的新事件(使用PHP API),php,codeigniter,google-calendar-api,google-oauth,Php,Codeigniter,Google Calendar Api,Google Oauth,我正在开发一个小应用程序,需要在初始日期更改后更新某些事件(法庭听证会)。用户可以通过访问其应用程序帐户在其谷歌日历中手动插入此类事件。一旦插入,应用程序将监控任何更改(针对公共API),一旦发生更改,它将更新数据库,将向用户发送一封电子邮件,通知更改并将此类新事件插入其谷歌日历中 我正在使用PHP和CodeIgniter3.11 任何更改每天都会根据公共API进行验证,显然,当更改发生时,用户不会出现在他的计算机前。这是在特定时间运行的cronjob的帮助下完成的 在用户的Google日历中首

我正在开发一个小应用程序,需要在初始日期更改后更新某些事件(法庭听证会)。用户可以通过访问其应用程序帐户在其谷歌日历中手动插入此类事件。一旦插入,应用程序将监控任何更改(针对公共API),一旦发生更改,它将更新数据库,将向用户发送一封电子邮件,通知更改并将此类新事件插入其谷歌日历中

我正在使用PHP和CodeIgniter3.11

任何更改每天都会根据公共API进行验证,显然,当更改发生时,用户不会出现在他的计算机前。这是在特定时间运行的cronjob的帮助下完成的

在用户的Google日历中首次插入事件运行正常,这与用户从浏览器手动执行的操作相同。但是,当我运行为监视cronjob下的更改而创建的控制器时,插入此类事件时遇到问题。我正在我自己的Gmail帐户上测试它

cronjob确实可以工作,因为控制器可以毫无问题地执行其他两项任务(更新数据库和发送电子邮件)。但更新日历从未发生过。只有通过浏览器再次运行控制器时,日历才会更新。进行监视的控制器负责设置oauth url并重定向到该url。然后我有另一个控件(位于redirect_uri后面),它应该接收访问代码和状态

我已经按照谷歌文档的建议将访问类型设置为离线

我已经保存并使用了一个刷新令牌,当用户不在谷歌文档中时,该令牌可用于获取访问代码

在我看来,当作为cronjob运行时,负责更新日历的控制器(在googleoauth客户端配置中设置为redirect_uri)永远不会到达。我的问题是:

  • 当用户不在浏览器中时,是否可以实现这一点,即在用户的谷歌日历中插入新事件?谷歌文档暗示了这种可能性,但没有详细解释如何做到这一点。引用文件:

  • 当用户不在场时,任何需要访问Google API的应用程序都需要请求脱机访问。例如,在预定时间执行备份服务或执行操作的应用程序需要能够在用户不在场时刷新其访问令牌


  • 我基于使用刷新令牌的方法是正确的,还是我需要执行其他任务以实现谷歌日历的更新

  • 当作为cronjob运行时,我如何发现我的控制器正在重定向到google服务器

    是否有任何服务器日志可以检查Monitor.php中的重定向设置是否正常工作?我有 检查了apache日志,但似乎没有出现任何错误。codeigniter日志未显示任何 也有错误

  • 我怎样才能发现负责更新的控制器是通过谷歌发送的响应到达的 服务器,有访问代码吗?如果google响应到达控制器,有什么方法可以调试吗 在重定向的背后

  • 如果您对如何实现这一目标有任何其他建议,我们非常欢迎您

    非常感谢

    下面是我的一些代码: Monitor.php(控制器作为cronjob运行)


    我知道您希望使用在另一个用户日历中创建事件。我假设你没有一个朋友。在这种情况下,需要在另一个用户日历中创建事件。您可以看到一个关于如何使用日历API在PHP中设置凭据的示例,您只需要使用新服务帐户中的凭据更改示例凭据。对于事件创建,您可以使用,但请记住,用户以前应该与服务帐户共享日历。在他们共享日历后,您应该使用上一种方法使用日历标识符创建日历。用户查找日历标识符的一种方法是进入,查看“我的日历”上的日历列表,然后单击其右侧并
    ⋮ 象征⮞ 设置和共享⮞ 日历ID(在“集成日历”下)
    。如果需要进一步帮助,请向我询问任何问题。

    我知道您想使用在其他用户日历中创建活动。我假设你没有一个朋友。在这种情况下,需要在另一个用户日历中创建事件。您可以看到一个关于如何使用日历API在PHP中设置凭据的示例,您只需要使用新服务帐户中的凭据更改示例凭据。对于事件创建,您可以使用,但请记住,用户以前应该与服务帐户共享日历。在他们共享日历后,您应该使用上一种方法使用日历标识符创建日历。用户查找日历标识符的一种方法是进入,查看“我的日历”上的日历列表,然后单击其右侧并
    ⋮ 象征⮞ 设置和共享⮞ 日历ID(在“集成日历”下)
    。如果需要进一步帮助,请询问我任何问题。

    可以将事件插入用户的日历,但用户必须授予此帐户访问其日历的权限。您是否考虑过使用?@Jescanella,因为应用程序可以访问日历(我正在测试我的gmail帐户),而且很明显,它可以插入和修改日历。在API Google控制台的服务帐户标题下,我有一个服务帐户,它是计算引擎服务帐户。在OAuth 2.0客户机ID标题下,我有一个服务帐户客户机,它是自动生成的,并且是与项目相关的项目所必需的
     $client->setAccessType('offline');
    
    <?php
    defined('BASEPATH') or exit('No direct script access allowed');
    
    /**
     * Class Monitor
     */
    class Monitor extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            $this->load->helper("utilities", "url");
            $this->load->model("users");
            $this->load->model("files");
            // $this->load->library("portal");
            require_once APPPATH . 'libraries/Portal.php';
            require_once 'vendor/autoload.php';
        }
    
        public function index()
        {
           if (!is_cli()) {
                return;
            }
    
            $files_query = $this->files->get_all_files();
    
            if (is_array($files_query)) {
                $files = array();
                foreach($files_query as $file_query) {
                    array_push($files, $file_query['file_number']);
                }
                $files = array_values(array_unique($files));
                //print_r($files);
                foreach ($files as $file) {
                    $email_body = "";
                    $portal = new Portal($file);
                    $portal->set_courtfile_data();
                    if (empty($portal->error)) {
                        $court_file = $portal->court_file; //obtaining the court file
                        $termen = $portal->get_last_hearing(); // obtaining the hearing
                        $solutie = $portal->get_solutie(); //obtaining the existing solutions
                        if (is_array($solutie)) {
                            $solutie = $solutie['final'];
                        }
                        $court = $portal->get_court(); //obtaining the court
                        $stadiu = $portal->get_courtfile_stage(); //obtaining the stage
                        if (!$stadiu["apel"]) {
                            $stadiu = $stadiu["stadiu"];
                        } else {
                            $stadiu = $stadiu["apel"];
                        }
            ####--- various checks made against the result retrieve from the public API ---####
            ......
           if ($email_body !== "") {
                    $emails = $this->get_user_email($file);
                    // Sending the e-mail to the user;
    
                    $template_id = $this->config->item('sendgrid_change_template');
                    $dynamic_data = [
                        "file" => $file,
                        "change_body" => $email_body
                    ];
                    foreach ($emails as $email) {
                        send_grid_email($email, $template_id, $dynamic_data);
                    }
                    if ($hearing_query) {
                        ###--- function for setting the google auth url ---####
                        $url = $this->set_google_url($court_file); 
                        try {
                            header('Location: ' . $url);
                            //redirect($url);
                        } catch (Exception $e) {
                            log_message('error', $e['message']);
                        }
                    }
                }
            } // End of foreach loop
        } else {
            $error = $this->db->error();
            log_message("error", $error["message"]);
        }
    }// End of index function
    
    #### -- function used to set the google oauth url ---###
    protected function set_google_url($file)
    {
        $client = new Google_Client();
        try {
            $client->setAuthConfig(APPPATH . 'credentials.json');
            $client->setApplicationName('Monitor App');
            $client->setRedirectUri($this->config->item('update_calendar_url'));
            $client->setScopes(Google_Service_Calendar::CALENDAR);
            $client->setState($file);
            $client->setAccessType('offline');
            $client->setApprovalPrompt('auto');
            $auth_url = $client->createAuthUrl();
            return filter_var($auth_url, FILTER_SANITIZE_URL);
        } catch (Exception $e) {
            log_message('error', $e['message']);
            return false;
        }
    
    }
    
    <?php
        defined('BASEPATH') or exit('No direct script access allowed');
    
        class Update_google extends CI_Controller
        {
            public function __construct()
            {
                parent::__construct();
                $this->load->helper("utilities");
                $this->load->model(array("files", "users"));
                require_once 'vendor/autoload.php';
            }
    
            public function index()
            {
                $query = $this->users->get_user_by("[user@gmail.com]", "email");
                if ($query) {
    
                    $refresh_token = $query[0]["refreshToken"];
                }
                try {
                    $client = new Google_Client();
                    $client->setAuthConfig(APPPATH . 'credentials.json');
                    $client->setRedirectUri($this->config->item('update_calendar_url'));
    
                    if (isset($_GET['state']) && isset($_GET['code'])) {
                        $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
                    } else {
                        if ($refresh_token != null) {
                            $token = $client->fetchAccessTokenWithRefreshToken($refresh_token);
                        }
                    }
    
                        $client->setAccessToken($token);
                        $file = filter_var($_GET['state'], FILTER_SANITIZE_STRING);
                        $query = $this->files->get_file_by($file, "file_number");
                        if (is_array($query)) {
                            $service = new Google_Service_Calendar($client);
                            $event = new Google_Service_Calendar_Event(array(
                                'summary' => 'Termen instanță',
                                'location' => $query[0]['court'],
                                'description' => 'Termen în dosarul ' . $query[0]['file_number'],
                                'start' => array(
                                    'dateTime' => $query[0]['last_hearing'] . 'T08:30:00',
                                    'timeZone' => 'Europe/Bucharest',
                                ),
                                'end' => array(
                                    'dateTime' => $query[0]['last_hearing'] . 'T12:00:00',
                                    'timeZone' => 'Europe/Bucharest',
                                ),
                            ));
                            $calendarId = 'primary';
                            $service->events->insert($calendarId, $event);
                        }
                } catch (Exception $e) {
                    log_message('error', $e['message']);
                }
            }
        }