PageRenderTime 173ms CodeModel.GetById 32ms app.highlight 9ms RepoModel.GetById 128ms app.codeStats 0ms

/Readme.md

http://github.com/cakebaker/oauth-consumer-component
Markdown | 77 lines | 54 code | 23 blank | 0 comment | 0 complexity | 2145a859b674cb6d7e6c903e29de5013 MD5 | raw file
 1# OAuth consumer component for CakePHP
 2
 3## Purpose
 4
 5An OAuth consumer component for CakePHP 2.x supporting OAuth 1.0 as defined in http://tools.ietf.org/html/rfc5849. For CakePHP 1.x, please checkout the [`cakephp_1.x` branch](https://github.com/cakebaker/oauth-consumer-component/tree/cakephp_1.x) and/or [download](https://github.com/cakebaker/oauth-consumer-component/zipball/v1.0.1) the latest version for it.
 6
 7## Installation
 8
 9* Copy the component and the `OAuthConsumers` folder to the `Controller/Component` folder of your application
10
11## Usage
12
13For each API you want to use, you have to write a consumer class. This class is responsible to handle the consumer key and consumer secret you get from the API provider (for using the Twitter API, as in this example, you have to register your application at https://twitter.com/oauth).
14
15The requirements for such a class are:
16
17* its name must end with `Consumer`
18* it must extend `AbstractConsumer`
19* it must be placed in the `OAuthConsumers` folder
20
21As you can see in the example below, a consumer class is pretty simple:
22
23```php
24<?php
25// Controller/Component/OAuthConsumers/TwitterConsumer.php
26class TwitterConsumer extends AbstractConsumer {
27  public function __construct() {
28    parent::__construct('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
29  }
30}
31```
32As usual in CakePHP, you have to add the component to the `$components` array of the controller(s) in which you want to use the component.
33
34In the `index` method a request token is obtained and the user is redirected to Twitter to authorize the request token. Notice the first parameter passed to the `getRequestToken` method, it is the name (without `Consumer`) of the previously created consumer class and tells the component which credentials to use for the request.
35
36In 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 get an access token over and over again.
37
38```php
39<?php
40// Controller/TwitterController.php
41class TwitterController extends AppController {
42  public $components = array('OAuthConsumer');
43
44  public function index() {
45    $requestToken = $this->OAuthConsumer->getRequestToken('Twitter', 'https://api.twitter.com/oauth/request_token', 'http://' . $_SERVER['HTTP_HOST'] . '/twitter/callback');
46
47    if ($requestToken) {
48      $this->Session->write('twitter_request_token', $requestToken);
49      $this->redirect('https://api.twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
50    } else {
51      // an error occured when obtaining a request token
52    }
53  }
54
55  public function callback() {
56    $requestToken = $this->Session->read('twitter_request_token');
57    $accessToken = $this->OAuthConsumer->getAccessToken('Twitter', 'https://api.twitter.com/oauth/access_token', $requestToken);
58
59    if ($accessToken) {
60      $this->OAuthConsumer->post('Twitter', $accessToken->key, $accessToken->secret, 'https://api.twitter.com/1/statuses/update.json', array('status' => 'hello world!'));
61    }
62    exit;
63  }
64}
65```
66
67## Migration from CakePHP 1.x to CakePHP 2.x
68
69If you are migrating your application to CakePHP 2.x, you have to make a few changes beside updating the component. First, you have to rename `OauthConsumer` to `OAuthConsumer` in the `$components` array and everywhere you are using the component. And second, you have to move all consumer classes to the new `OAuthConsumers` folder and camel-case the file names, i.e. `twitter_consumer.php` becomes `TwitterConsumer.php`.
70
71## Contact
72
73If you have questions or feedback, feel free to contact me via Twitter ([@dhofstet](https://twitter.com/dhofstet)) or by email (daniel.hofstetter@42dh.com).
74
75## License
76
77The OAuth consumer component is licensed under the MIT license.