Home > Zend_Gdata > Accessing Google Calendar using Zend_Gdata_AuthSub

Accessing Google Calendar using Zend_Gdata_AuthSub

intro
This article shows code that access your google calendar using Zend_Gdata_AuthSub and Zend_Gdata_Calendar.
Please see Programmer's Reference Guide(16.2) for more detail information.
You can check the screen from demonstration site(though I don't know about security).

1.Create Controller
Create the file application/constorllers/GdataController.php with the following content.

<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Gdata.php';
require_once 'Zend/Gdata/AuthSub.php';
require_once 'Zend/Gdata/Calendar.php';
require_once 'Zend/Session/Namespace.php';
 
class GdataController extends Zend_Controller_Action
{
    protected $_session;
 
    public function preDispatch()
    {
        $this->_session = new Zend_Session_Namespace('GdataController');
    }
 
    public function indexAction()
    {
        $my_calendar = 'http://www.google.com/calendar/feeds/default/private/full';
        if (!isset($this->_session->cal_token)) {
            if (isset($_GET['token'])) {
                // You can convert the single-use token to a session token.
                $token = $_GET['token'];
                $session_token =  Zend_Gdata_AuthSub::getAuthSubSessionToken($token);
                // Store the session token in our session.
                $this->_session->cal_token = $session_token;
            } else {
                // Display link to generate single-use token
                $googleUri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
                    'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
                    $my_calendar, 0, 1);
                $this->view->message =
                    "Go <a href='$googleUri'>Google Login Screen</a>";
                return $this->render();
            }
        }
 
        // Create an authenticated HTTP Client to talk to Google.
        $client = Zend_Gdata_AuthSub::getHttpClient($this->_session->cal_token);
 
        // Create a Gdata object using the authenticated Http Client
        $service = new Zend_Gdata_Calendar($client);
        $query = $service->newEventQuery();
        $query->setUser('default');
        $query->setVisibility('private');
        $query->setProjection('full');
        $query->setOrderby('starttime');
        $query->setFutureevents('true');
 
        $this->view->events = $service->getCalendarEventFeed($query);
    }
 
    public function logoutAction()
    {
        if (isset($this->_session->cal_token))
        {
            Zend_Gdata_AuthSub::AuthSubRevokeToken($this->_session->cal_token);
            unset($this->_session->cal_token);
        }
        $this->_helper->redirector('index');
    }
}
2.Create View
Create the file application/views/scripts/gdata/index.phtml with the following content.

<?php if (isset($this->message)) : ?>
<?= $this->message ?>
<?php endif; ?>
<?php if (isset($this->events)) : ?>
<h1>Calendar Event List</h1>
<ul>
<?php foreach ($this->events as $event) : ?>
<li><?= $event->title?>(Event ID:<?= $event->id ?>)</li>
 
<?php endforeach; ?>
</ul>
 
<?php endif; ?>
<div>
<a href="<?= $this->url(array('action'=>'logout'))?>">Logout</a>
</div>
 

3.Check
Access the web server and check events of your google calendar after you logged in from google's login screen.
History
Date Content
2008/5/1 Published

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/39/trackback
Listed below are links to weblogs that reference
Accessing Google Calendar using Zend_Gdata_AuthSub from Open Programming Laboratory

Home > Zend_Gdata > Accessing Google Calendar using Zend_Gdata_AuthSub

Japanese
Search
Feeds

Return to page top