Codeigniter OAuth 2.0

You can interact with any provider which is supported in the list below:

  • Facebook
  • Foursquare
  • GitHub
  • Google
  • PayPal
  • Instagram
  • Soundcloud
  • Windows Live
  • YouTube

Please follow the below steps for OAuth 2 integaration :-

  1. download liberary form github (https://github.com/philsturgeon/codeigniter-oauth2).
  2. paste in application/libraries.
  3. set in auto load config/autoload.php and set like (
    $autoload['libraries'] = array('database', 'session','template','OAuth2','oauth/OAuth','Phpflickr','nic');

    )

  4. now create one controller and paste this code in your controller.In my case name of controller is Sociallogin.
    <?php  class social extends CI_Controller {
      public function __construct() {
         parent::__construct();
           $this->load->helper('form');
    
           $this->load->library('session');
            $this->load->helper('url');
            $this->load->model('commonmodel');
    		$this->load->model('usersmodel');
    
    	}//end __construct()
    
        public function session($provider) { 
    
        	 $this->load->helper('url_helper');
             //facebook
             if($provider == 'facebook') {
                    $app_id = $this->config->item('fb_appid');
    		$app_secret = $this->config->item('fb_appsecret');		
    		$provider	= $this->oauth2->provider($provider, array(
    			'id' => $app_id,
    			'secret' => $app_secret,
    			));			
    		}
    	//google
    		else if($provider == 'google'){
    
    			$app_id 		= $this->config->item('googleplus_appid');
    			$app_secret 	= $this->config->item('googleplus_appsecret');
    			$provider 		= $this->oauth2->provider($provider, array(
    				'id' => $app_id,
    				'secret' => $app_secret,
    			)); 			
    		}
    
    	//foursquare
    		else if($provider == 'foursquare'){
    
    			$app_id 	= $this->config->item('foursquare_appid');
    			$app_secret = $this->config->item('foursquare_appsecret');
    			$provider 	= $this->oauth2->provider($provider, array(
    				'id' => $app_id,
    				'secret' => $app_secret,
    			)); 			
    		}
    
    	if ( ! $this->input->get('code'))
            {  
                // By sending no options it'll come back here
                $provider->authorize();
                redirect('social?error');
            }
            else
            {
                // Howzit?
                try
                {
                    $token = $provider->access($_GET['code']);
                    $user = $provider->get_user_info($token);
    
                    if($this->uri->segment(3) == 'google'){
                         //Your code stuff here 
                    }
    
                    elseif($this->uri->segment(3) == 'facebook'){
                        //your facebook stuff here         
    
                	}elseif($this->uri->segment(3) == 'foursquare'){
                        // your code stuff here
                    }
    
            	$this->session->set_flashdata('info',$message);
                    redirect('social?tabindex=s&status=sucess');
    
                }
    
                catch (OAuth2_Exception $e)
                {
                    show_error('That didnt work: '.$e);
                }
    
            }
        }}
  5. you can access you your controller like this
    http://host/social/session/providername
  6. provider name may be facebook or google or etc………
  7. $user = $provider->get_user_info($token);print_r($user); will show the array of userdata
Advertisements