/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. ## Purpose
  3. An 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.
  4. ## Installation
  5. * Copy the component and the `OAuthConsumers` folder to the `Controller/Component` folder of your application
  6. ## Usage
  7. For 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).
  8. The requirements for such a class are:
  9. * its name must end with `Consumer`
  10. * it must extend `AbstractConsumer`
  11. * it must be placed in the `OAuthConsumers` folder
  12. As you can see in the example below, a consumer class is pretty simple:
  13. ```php
  14. <?php
  15. // Controller/Component/OAuthConsumers/TwitterConsumer.php
  16. class TwitterConsumer extends AbstractConsumer {
  17. public function __construct() {
  18. parent::__construct('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
  19. }
  20. }
  21. ```
  22. As 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.
  23. In 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.
  24. 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 get an access token over and over again.
  25. ```php
  26. <?php
  27. // Controller/TwitterController.php
  28. class TwitterController extends AppController {
  29. public $components = array('OAuthConsumer');
  30. public function index() {
  31. $requestToken = $this->OAuthConsumer->getRequestToken('Twitter', 'https://api.twitter.com/oauth/request_token', 'http://' . $_SERVER['HTTP_HOST'] . '/twitter/callback');
  32. if ($requestToken) {
  33. $this->Session->write('twitter_request_token', $requestToken);
  34. $this->redirect('https://api.twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
  35. } else {
  36. // an error occured when obtaining a request token
  37. }
  38. }
  39. public function callback() {
  40. $requestToken = $this->Session->read('twitter_request_token');
  41. $accessToken = $this->OAuthConsumer->getAccessToken('Twitter', 'https://api.twitter.com/oauth/access_token', $requestToken);
  42. if ($accessToken) {
  43. $this->OAuthConsumer->post('Twitter', $accessToken->key, $accessToken->secret, 'https://api.twitter.com/1/statuses/update.json', array('status' => 'hello world!'));
  44. }
  45. exit;
  46. }
  47. }
  48. ```
  49. ## Migration from CakePHP 1.x to CakePHP 2.x
  50. If 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`.
  51. ## Contact
  52. If you have questions or feedback, feel free to contact me via Twitter ([@dhofstet](https://twitter.com/dhofstet)) or by email (daniel.hofstetter@42dh.com).
  53. ## License
  54. The OAuth consumer component is licensed under the MIT license.