没有用户身份验证的Freebusy Google Calendar API PHP

时间:2016-07-01 14:08:03

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

我正在创建一个使用Google日历的预订引擎。

需要:

  1. 在时间范围内(日,周,月)获取所有活动
  2. 检查时间范围的可用性,freebusy(30分钟,小时)
  3. 如果#2为真,则发布事件
  4. 目前,我可以使用我在this tutorial之后创建的命令行应用来获取事件并检查可用性。但它仍然要求我在浏览器中进行身份验证,这不是我想要的,因为日历总是相同的,用户将无法访问它。

    到目前为止我的代码:

    <?php
    require_once 'vendor/autoload.php';
    
    define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
    define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json');
    define('CLIENT_SECRET_PATH', 'client_secret.json');
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/calendar-php-quickstart.json
    define('SCOPES', implode(' ', array(
      Google_Service_Calendar::CALENDAR_READONLY)
    ));
    
    if (php_sapi_name() != 'cli') {
      throw new Exception('This application must be run on the command line.');
    }
    
    /**
     * Returns an authorized API client.
     * @return Google_Client the authorized client object
     */
    function getClient() {
      $client = new Google_Client();
      $client->setApplicationName(APPLICATION_NAME);
      $client->setScopes(SCOPES);
      $client->setAuthConfigFile(CLIENT_SECRET_PATH);
      $client->setAccessType('offline');
    
      // Load previously authorized credentials from a file.
      $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
      if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
      } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));
    
        // Exchange authorization code for an access token.
        $accessToken = $client->authenticate($authCode);
    
        // Store the credentials to disk.
        if(!file_exists(dirname($credentialsPath))) {
          mkdir(dirname($credentialsPath), 0700, true);
        }
        file_put_contents($credentialsPath, $accessToken);
        printf("Credentials saved to %s\n", $credentialsPath);
      }
      $client->setAccessToken($accessToken);
    
      // Refresh the token if it's expired.
      if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
      }
      return $client;
    }
    
    /**
     * Expands the home directory alias '~' to the full path.
     * @param string $path the path to expand.
     * @return string the expanded path.
     */
    function expandHomeDirectory($path) {
      $homeDirectory = getenv('HOME');
      if (empty($homeDirectory)) {
        $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
      }
      return str_replace('~', realpath($homeDirectory), $path);
    }
    
    // Get the API client and construct the service object.
    $client = getClient();
    $service = new Google_Service_Calendar($client);
    
    // Print the next 10 events on the user's calendar.
    $calendarId = 'pgsgh5dp4a5seur91fvn86bn3o@group.calendar.google.com';
    $optParams = array(
      'maxResults' => 10,
      'orderBy' => 'startTime',
      'singleEvents' => TRUE,
      'timeMin' => '2005-08-15T15:52:01+0000',
    );
    $results = $service->events->listEvents($calendarId, $optParams);
    
    if (count($results->getItems()) == 0) {
      print "No upcoming events found.\n";
    } else {
      print "Upcoming events:\n";
      foreach ($results->getItems() as $event) {
        $start = $event->start->dateTime;
        if (empty($start)) {
          $start = $event->start->date;
        }
        printf("%s (%s)\n", $event->getSummary(), $start);
      }
    }
    ?>
    

    后续步骤:

    1. 如何跳过客户端身份验证?
    2. 如何将所有内容转换为Web应用程序而不是命令行1?

0 个答案:

没有答案