$oauthParams .= $key . '="' . urlencode($value) . '"';
        $first = false;
    }
}
/**
 * Generate SASL client request, using base64 encoded 
 * OAuth params
 */
$initClientRequest = 'GET ' . $urlWithXoauth . ' ' . $oauthParams;
$initClientRequestEncoded = base64_encode($initClientRequest);
/**
 * Make the IMAP connection and send the auth request
 */
$imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
$authenticateParams = array('XOAUTH', $initClientRequestEncoded);
$imap->requestAndResponse('AUTHENTICATE', $authenticateParams);
/**
 * Print the INBOX message count and the subject of all messages
 * in the INBOX
 */
$storage = new Zend_Mail_Storage_Imap($imap);
include 'header.php';
echo '<h1>Total messages: ' . $storage->countMessages() . ' for ' . $TWO_LEGGED_EMAIL_ADDRESS . "</h1>\n";
/**
 * Retrieve first 5 messages.  If retrieving more, you'll want
 * to directly use Zend_Mail_Protocol_Imap and do a batch retrieval,
 * plus retrieve only the headers
 */
echo 'First five messages: <ul>';
for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++) {
    echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n";