Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
OAuth consumer for CakePHP realized as vendor class
branch: master

Merge pull request #7 from martinbean/patch-1

Check if OAuthException class is already defined
latest commit c1a1be4f4a
@cakebaker authored

OAuth client class for CakePHP


An OAuth client class for CakePHP 2.x supporting OAuth 1.0 as defined in For CakePHP 1.x, please checkout the cakephp_1.x branch and/or download the latest version for it.


  • Place all files into an OAuth folder in the Vendor folder of your application


To use the OAuth client class, you have to import it with App::import().

Before you can instantiate the client class, you have to register your application with your API provider to get consumer key and consumer secret (for this example you have to register your application at Consumer key and consumer secret are required as parameters for the constructor. In the example below I moved the instantiation of the client class to a private method createClient() to avoid code duplication.

In the index method a request token is obtained and the user is redirected to Twitter to authorize the request token.

In the callback method the request token is exchanged for an access token. Using this access token, a new status is posted to Twitter. Please note that in a real application, you would save the access token data in a database to avoid that the user has to go through the process of getting an access token over and over again.

// Controller/TwitterController.php
App::import('Vendor', 'OAuth/OAuthClient');

class TwitterController extends AppController {
  public function index() {
    $client = $this->createClient();
    $requestToken = $client->getRequestToken('', 'http://' . $_SERVER['HTTP_HOST'] . '/twitter/callback');

    if ($requestToken) {
      $this->Session->write('twitter_request_token', $requestToken);
      $this->redirect('' . $requestToken->key);
    } else {
      // an error occured when obtaining a request token

  public function callback() {
    $requestToken = $this->Session->read('twitter_request_token');
    $client = $this->createClient();
    $accessToken = $client->getAccessToken('', $requestToken);

    if ($accessToken) {
      $client->post($accessToken->key, $accessToken->secret, '', array('status' => 'hello world!'));

  private function createClient() {
    return new OAuthClient('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');

Migration from CakePHP 1.x to CakePHP 2.x

If you are migrating your application to CakePHP 2.x, you have to make a few changes beside updating the client class and the OAuth library. First, you have to change App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'oauth_consumer.php')); to App::import('Vendor', 'OAuth/OAuthClient');. And second, you have to rename the class from OAuth_Consumer to OAuthClient when instantiating it.


If you have questions or feedback, feel free to contact me via Twitter (@dhofstet) or by email (


The OAuth client class is licensed under the MIT license.

Something went wrong with that request. Please try again.