【问题标题】:Google Calendar API (v3) - PHP can write but not read?Google Calendar API (v3) - PHP 能写但不能读?
【发布时间】:2014-07-26 17:51:10
【问题描述】:

我花了一整天时间研究 Google Calendar API。我终于设法在我的 Google 日历中插入了一个活动,但现在我似乎无法让“列表”命令正常工作。

以下代码有效

    <?php
    $start = array(
        "dateTime" => $date . "T" . $start_time . ":00",
        "timeZone" => "Europe/Berlin"
    );

    $end = array(
        "dateTime" => $date . "T" . $end_time . ":00",
        "timeZone" => "Europe/Berlin"
    );

    $headerarray = array(
        'Content-type: application/json',
        'Authorization: Bearer ' . $access_token,
        'X-JavaScript-User-Agent: Google APIs Explorer'
    );

    $post_data = array(
        "start"       => $start,
        "end"         => $end,
        "summary"     => $title,
        "description" => $description,
        "key"         => $api_key
    );

    $post_data = json_encode($post_data);

    $url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $response = json_decode($response);
    ?>

这段代码在我的日历中创建了一个新事件,所以我应该正确设置所有内容,对吧?

但是这段代码工作:

    <?php
    $headerarray = array(
        "Authorization: Bearer " . $access_token,
        "X-JavaScript-User-Agent: Google APIs Explorer"
    );

    $url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events?key=' . $api_key;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $response = json_decode($response);
    ?>

在这种情况下,我收到以下回复:Access Not Configured. Please use Google Developers Console to activate the API for your project.

但事实并非如此。我正确配置了访问权限,否则我将无法将事件插入日历,对吗?也许我没有使用 cURL 对吗?

这是列表函数的参考:https://developers.google.com/google-apps/calendar/v3/reference/events/list

我在这里没有看到什么?非常感谢任何帮助。

【问题讨论】:

  • 好吧,没关系!现在可以了。我不知道为什么。我不知道为什么它以前不起作用。我没有做任何改变,它突然起作用了。 -.-
  • 为什么在你的第一个例子中使用curlopt_httpgetcurlopt_post

标签: php curl calendar google-api google-calendar-api


【解决方案1】:

试试这个:

【讨论】:

    【解决方案2】:

    我花了一整天的时间来创建事件,我能够获取事件

    这是我从 google oauth 获取数据的代码,我正在使用 codeigniter 框架。

    <?php
        // product_config
         $config['google_id'] = '';
         $config['google_secret'] = '';
    
        // oauth file
        function get_calendar_events(OAuth2_Token_Access $token){
            try{
    
                $max_results = 250;
                $url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?showDeleted=false&$orderBy=email&maxResults='.$max_results.'&alt=json&v=3.0&oauth_token='.$token->access_token;
                $google_events = json_decode(file_get_contents($url), true);
    
                return $google_events;
    
            }catch (Exception $e) {
                // Exception
            }
        }
    
        /*** Controller ***/
        function myCalendar() {
            try {
                $events = array();
                $provider_name = 'google';
                $provider = $this->oauth2->provider($provider_name, array(
                        'id' => $this->config->item($provider_name.'_id', 'product_config'),
                        'secret' => $this->config->item($provider_name.'_secret', 'product_config'),
                        'scopes' =>  array('https://www.googleapis.com/auth/calendar',
                                    'https://www.googleapis.com/auth/calendar.readonly')
                ));
    
                if (!$this->input->get('code')){ // access authorizing
                    // By sending no options it'll come back here
                    $provider->authorize();
                }else{
                    // Get the Token
                    $token = $provider->access($this->input->get('code'));
                    $events  = $provider->get_calendar_events($token);
            }catch (Exception $e) {
                // Exception
            }
        }
    ?>
    

    【讨论】:

      【解决方案3】:
      public function create_calendar_event(OAuth2_Token_Access $token,$description, $leaveDate , $toLeaveDate,$title, $stime, $etime ){
          try {
      
              $title = $title;
      
              //$start_time = '00:00'; $end_time = '23:59';
              $start_time = $stime;
              $end_time = $etime;
              $timezone       = 'Asia/Kolkata';
      
              $start = array(
                  "dateTime" => $leaveDate . "T" . $start_time . ":00",
                  "timeZone" => $timezone
              );
      
              $end = array(
                  "dateTime" => $toLeaveDate . "T" . $end_time . ":00",
                  "timeZone" => $timezone
              );
      
              $headerarray = array(
                  'Content-type: application/json',
                  'Authorization: Bearer ' . $token->access_token,
                  'X-JavaScript-User-Agent: Google APIs Explorer'
              );
      
              $post_data = array(
                      "start"       => $start,
                      "end"         => $end,
                      "summary"     => $title,
                      "description" => $description,
                      "key"         => $this->api_key
              );
      
              $post_data = json_encode($post_data);
              $calendar_id = 'primary';
      
              $url = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';
      
              $result = $this->NewcurlRequest($url, $headerarray, $post_data);
      
          }catch(Exception $e){
              // Exception
          }
      }
      public function NewcurlRequest($url,$headerarray,$post_data, $curl_call = true){
          try{
      
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, 1);
              //curl_setopt($ch, curlopt_post, true);
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
              curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
              $response = curl_exec($ch);
              curl_close($ch);
      
              return $response;
          } catch (Exception $e) {
              return $e;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 2017-04-10
        相关资源
        最近更新 更多