
I currently provide two slightly different ways to consume OAuth-enabled APIs with CakePHP: a component and a vendors class. The vendors class emerged from the component and is a bit more flexible than the component.
Both classes require PHP5 and CakePHP 1.2/1.3, and they depend on the (open source) PHP library for OAuth by Andy Smith. The classes are licensed under the MIT license.

(latest release: 2009-09-05, see also the changelog)
Or you can get it with: git clone git://github.com/cakebaker/oauth-consumer.git
This example performs the following steps:
In a real application you would store the access token (resp. its key and secret) in a database. Please notice that the class name of the vendors class doesn't follow the naming conventions of CakePHP: it is "OAuth_Consumer" and not "OauthConsumer" (to avoid a naming conflict with the OAuth library).
// app/controllers/example_controller.php
App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'oauth_consumer.php'));
class ExampleController extends AppController {
public $uses = array();
public function twitter() {
$consumer = $this->createConsumer();
$requestToken = $consumer->getRequestToken('http://twitter.com/oauth/request_token', 'http://test.localhost/example/twitter_callback');
$this->Session->write('twitter_request_token', $requestToken);
$this->redirect('http://twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
}
public function twitter_callback() {
$requestToken = $this->Session->read('twitter_request_token');
$consumer = $this->createConsumer();
$accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken);
$consumer->post($accessToken->key, $accessToken->secret, 'http://twitter.com/statuses/update.json', array('status' => 'hello world!'));
}
private function createConsumer() {
return new OAuth_Consumer('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
}
}

(latest release: 2009-09-05, see also the changelog)
Or you can get it with: git clone git://github.com/cakebaker/oauth-consumer-component.git
The OAuth consumer component requires for each API you want to use a consumer class which is responsible to handle consumer key and consumer secret. You get those data when you register your application at the respective service (for this example you have to register your application at http://twitter.com/oauth).
// app/controllers/components/oauth_consumers/twitter_consumer.php
class TwitterConsumer extends AbstractConsumer {
public function __construct() {
parent::__construct('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
}
}
The main difference to the example with the OAuth vendors class is that you always have to provide the consumer name (in this example "Twitter"). Please notice that the component is named "OauthConsumer" and not "OAuthConsumer".
// app/controllers/example_controller.php
class ExampleController extends AppController {
public $uses = array();
public $components = array('OauthConsumer');
public function twitter() {
$requestToken = $this->OauthConsumer->getRequestToken('Twitter', 'http://twitter.com/oauth/request_token', 'http://test.localhost/example/twitter_callback');
$this->Session->write('twitter_request_token', $requestToken);
$this->redirect('http://twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
}
public function twitter_callback() {
$requestToken = $this->Session->read('twitter_request_token');
$accessToken = $this->OauthConsumer->getAccessToken('Twitter', 'http://twitter.com/oauth/access_token', $requestToken);
$this->OauthConsumer->post('Twitter', $accessToken->key, $accessToken->secret, 'http://twitter.com/statuses/update.json', array('status' => 'hello world!'));
}
}
If you have questions, feedback, or you simply want to say "hi", contact me via Twitter (@dhofstet) or send me an email (daniel.hofstetter at 42dh.com). Have fun :)
To learn more about OAuth in general, have a look at the OAuth specification, or wait for the upcoming book OpenID: The Definitive Guide from O'Reilly (despite its name it will not only cover OpenID but also OAuth).