Tumblr OAuth: Authenticate with tumblr in Codeigniter

Tumblr OAuth: Authenticate with tumblr in Codeigniter

In this post I would like to show how can you validate user with tumblr OAuth. Tumblr oauth login API allow to authenticate user into web applications by using their tumblr account. Follow me I will show you step by step how we can achieve this.

1.) Register application:

First of all you need to register your application in tumblr. To get register your app in tumblr Click here. Fill all the details and after that you will get consumer key and secret key that you need to apply in oauth setup.

Register app to get keys from tumblr oauth

2.) Installing tumblr OAuth Library:

Install tumblr oauth library using composer into codeigniter application.composer require tumblr/tumblr

3.) Setup tumblr oauth in codeigniter:

After successfully registered you will get oauth consumer key and secret key.

Consumer and secret key (Tumblr oauth key)

Now let see how can you setup tumblr oauth. Please find below the controller’s code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class TumblrLoginController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
Public function index()
{
$consumerKey = "YOUR_CONSUMER_KEY";
$consumerSecret = "CONSUMER_SECRET KEY";

$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');

// If user visiting the first time
if (!$_GET['oauth_verifier']) {
// grab the oauth token
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);

$oauthData = array(
't' => $data['oauth_token'],
's' => $data['oauth_token_secret']
);

$this->session->set_userdata($oauthData);
echo '<a href="https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'].'"> Login With Tumblr </a>';
} else {
$verifier = $_GET['oauth_verifier'];
// use the stored tokens
$client->setToken($this->session->userdata('t'), $this->session->userdata('s'));
// to grab the access tokens
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// and print out our new keys we got back
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
//echo "token: " . $token . "<br/>secret: " . $secret;

// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "<br/><br/>congrats " . $info->user->name . "!";
}
}
}

This example simply authenticate with Tumblr OAuth. You can use this for your application’s login system by checking user if new and storing necessary data into your database.

You may also like this posts:
i.) Authenticate with Facebook 

ii.) Authenticate with Gmail.Gmail.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!