- 2012/01/07

Zend_Service_* is sets of components to get data from another provider (web service) around the web. But, after waiting some time to get Zend_Service_Dropbox approved, I found that component still not alive / still no progress :( until I write this post. On Dropbox API online documentation, there is PHP SDK (unofficial) to access Dropbox ReST API (which is clear and great). Wait…. In Zend Framework, Twitter service is accessed via OAuth consumer implementation. Is that the same thing with this? (Dropbox), YES. Then I get my hands dirty to hack around Zend_Service_Twitter, and I found it is possible to implement it to Dropbox.

So I wrote some simple wrapper for Dropbox API using Zend_Oauth_Consumer, and I want to use it just like when I use Zend_Service_Twitter (I’m familiar with that :D). Here’s the result. exactly the same as when you use Zend_Service_Twittter.

Login, Callback, and Logout

<?php
public function loginAction()
{
    $session = $this->_getSession();
    $dropbox = $this->_getDropbox();
    
    $session->requestToken = $dropbox->getRequestToken();
    $dropbox->redirect();
}
 
public function logoutAction()
{
    $this->_getSession()->unsetAll();
    $this->_helper->redirector->gotoSimpleAndExit('index');
}
 
public function callbackAction()
{
    $session = $this->_getSession();
    $dropbox = $this->_getDropbox();
    
    $query = $this->getRequest()->getQuery();
    $requestToken = $session->requestToken;
    
    if(! empty($query) && null !== $requestToken) {
        $accessToken = $dropbox->getAccessToken($query, $requestToken);
        $session->accessToken = $accessToken;
        unset($this->_session->requestToken);
        $this->_helper->redirector->gotoSimpleAndExit('index');
    }
}

Now, this is where we get $dropbox and $session

<?php
protected $_dropbox;
protected $_session;
/**
 * @return ZendX_Service_Dropbox
 */
protected function _getDropbox()
{
    $options = array(
        'callbackUrl'    => $this->view->serverUrl() . $this->_helper->url->simple('callback'),
        // https://api.dropbox.com/developers/apps
        'consumerKey'    => 'YOUR_CONSUMER_KEY',
        'consumerSecret' => 'YOUR_CONSUMER_KEY_SECRET',
    );
    
    if(null === $this->_dropbox) {
        $session = $this->_getSession();
        
        if($session->accessToken && $session->accessToken instanceof Zend_Oauth_Token_Access) {
            $options['accessToken'] = $session->accessToken;
        }
        $this->_dropbox = new ZendX_Service_Dropbox($options);
    }
    return $this->_dropbox;
}
 
/**
 * Start your session earlier via Zend_Session::start() (eg. Application Bootstrap)
 * 
 * @return Zend_Session_Namespace
 */
protected function _getSession()
{
    // THIS IS FOR TESTING PURPOSE ONLY
    Zend_Session::start(array('save_path' => dirname(__FILE__)));
    
    if(null === $this->_session) {
        $this->_session = new Zend_Session_Namespace('dropbox');
    }
    return $this->_session;
}

With these implementation, you probably want to save oauth_token and oauth_token secret to another static file config or database instead of session (for later use). These Dropbox client, implements all ReST method provided, except POST /files because in API v1, they wrote that file PUT method is recommended than file POST.

You can grab these rest api client from Github. Any positive comments are welcome.