📜 ⬆️ ⬇️

Integration of OAuth and Twitter API in the framework Kohana3

Somehow I got into my head the thought that it was time to start integrating blog and twitter into a single whole. To do this, there are two such wonderful things as OAuth, which connects your application with the functionality of a third-party API, and the functionality itself, implemented, in this case, in the form of the Twitter API.

After studying several manuals (for example, Dmitry Koterov has a very good article on this topic) and having understood the basic principle of OAuth, I came to the conclusion that writing my bicycle is an inefficient task at the moment, and decided to see what is already ready .

So, what do we see by going to http://dev.twitter.com/pages/libraries ? Unimaginable number of ready-made solutions for our problem. I decided to stop at the Twitter-async library from Jaisen Mathai that I liked. Next, I will describe how to integrate the library into Kohana 3 and how to use it.

To get started, register your app on Twitter at http://twitter.com/apps/new . Fill in the fields, select the type "Browser" for "Application Type", "Default Access type" set in "Read & Write". Be sure to fill out the "Callback URL" field - the page where the redirect will be sent to your website after the exchange of secret keys between your application and twitter. For example, I have a separate twitter controller with the confirm method for which I will give the code later (that is, it looks like this: sitename.com/twitter/confirm ). Everything, we fill captcha, we press "Save" and we receive couple of keys - "Consumer key" and "Consumer secret". They will be needed later.
')
The next step is to unpack the contents of the archive with the Twitter-async library into the / modules / epitwitter / classes / directory of your website on kohana3. We need only three files - EpiCurl.php, EpiOAuth.php and EpiTwitter.php. For example, the path to EpiTwitter.php will look like this: / modules/epitwitter/classes/EpiTwitter.php. File name EpiTwitter.php translate into lower case (epitwitter.php) and add three lines to it right before the description of the class itself:


include_once( 'EpiCurl.php' );
include_once( 'EpiOAuth.php' );
include_once( 'secret.php' );
class EpiTwitter extends EpiOAuth
{


* This source code was highlighted with Source Code Highlighter .


These manipulations are needed to integrate the library into the Kohana3 module loading system.

Next, create the file / modules / epitwitter/classes/secret.php, where we will store our keys:

<?php
define( 'CONSUMER_KEY' , '__consumer_key' );
define( 'CONSUMER_SECRET' , '__consumer_secret' );
?>


* This source code was highlighted with Source Code Highlighter .


Open our bootstrap.php and add a line to it to load the library into the system:

Kohana::modules(array(

'epitwitter' => MODPATH. 'epitwitter' ,

));


* This source code was highlighted with Source Code Highlighter .


All, we have completed the integration of the library into the system, now it's time to make it work.

Creating a controller /application/classes/controller/twitter.php - for CallbackURL, remember? Here is how he looks at me:

<?php defined( 'SYSPATH' ) or die( 'No direct script access.' );
class Controller_Twitter extends Controller {

public function action_confirm()
{
$twitterObj = new EpiTwitter(CONSUMER_KEY, CONSUMER_SECRET);
$twitterObj->setToken($_GET[ 'oauth_token' ]);
$token = $twitterObj->getAccessToken();
Cookie::set( 'oauth_token' ,$token->oauth_token,60*60*24*7);
Cookie::set( 'oauth_token_secret' ,$token->oauth_token_secret,60*60*24*7);
header( 'Location: /' );
exit();
}

}


* This source code was highlighted with Source Code Highlighter .


In particular, we are interested in the confirm - public function action_confirm () method referenced by your application's CallbackURL setting. Here you get a token from OAuth Twitter and save its value (oauth_token and oauth_token_secret pairs), for example, in cookies. Then there is a redirect to the main page so that the cookies are initialized.

What happens on the main page? I have a Core controller for the core of the site.

<?php defined( 'SYSPATH' ) or die( 'No direct script access.' );
class Controller_Core extends Controller_Template {
public $template = 'main' ; //

public $twitterObj = NULL;
public $twitterInfo = NULL;

public function before()
{
parent::before();
// oAuth
if (isset($_COOKIE[ 'oauth_token' ]) and isset($_COOKIE[ 'oauth_token_secret' ]))
{
$twitterObj = new EpiTwitter(CONSUMER_KEY, CONSUMER_SECRET,
Cookie::get( 'oauth_token' ), Cookie::get( 'oauth_token_secret' ));
// oAuth+TwitterAPI .
$ this ->twitterObj = $twitterObj;
//
$twitterInfo = $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
$ this ->twitterInfo = $twitterInfo;
$ this ->template->twitterObj = $ this ->twitterObj;

// . .
$ this ->template->twitterInfo = $twitterInfo;
}
else
{
// EpiTwitter -
$twitterObj = new EpiTwitter(CONSUMER_KEY, CONSUMER_SECRET);
$ this ->twitterObj = NULL;
$ this ->twitterInfo = NULL;
// , :
$ this ->template->twitter_login = $twitterObj->getAuthorizationUrl();

}
}


* This source code was highlighted with Source Code Highlighter .



The Kohana3 framework has a wonderful before () method, which is remarkable in that it takes place before the activation of the main method. That is, before processing the main data of the main method and template rendering, instances of our library classes are created, the necessary key manipulations are performed and, depending on the circumstances, either a link to the authorization on oAuth Twitter is sent to the template, or information about an authorized user.

Our library has a very convenient way to access TwitterAPI. For example, consider this line:

$twitterInfo = $twitterObj->get_accountVerify_credentials();

What's going on here? Everything is very simple. We tell our object: using the GET method, tweet TwitterAPI at account / verify_credentials. The result is returned to json and parsed inside the object.

For example, to find out the public data of a Twitter account, according to TwitterAPI, you need to use the users / show method (see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2% A0show ). Accordingly, the call will look like this:

$twitterInfo = $twitterObj->get_usersShow(array('screen_name'=>'VasyaPupkin'));

It's simple :)

If you throw out the specification regarding integration into Kohana3, then briefly the bundle of your application + OAuth + Twitter API (using the Twitter-async library) looks like this:
  1. We register our application on Twitter, we get a couple of keys - consumer_key and consumer_secret
  2. In callbackurl we prepare a seat to get a token from OAuth Twitter, and save the tokens received somewhere for permanent authorization on our site.
  3. Using the resulting pair of tokens and a pair of consumer keys, we create an instance of EpiTwitter, with which you can freely access the Twitter API. Or, if tokens are missing, using a pair of consumer_key and consumer_secret, get a link to the authorization in oAuth Twitter to get tokens.

Source: https://habr.com/ru/post/92748/


All Articles